10

I am writing a script to read the output of a command to variable a and b. This is the script

#!/bin/bash

read a b < <(awk '/Application Server/ && !seen[$7]++{printf "%s ", $7}' /tmp/ServerState)

echo "The value of a is $a"
echo "The value of b is $b"

and getting the syntax error as :

line 3: syntax error near unexpected token `<'
line 3: `read a b < <(awk /Application Server/ && !seen[$7]++{echo "%s ", $7} /tmp/ServerState)'

But when I am typing the same command in the console it is working for me without any issue.

app@user:/tmp> read a b < <(awk '/Application Server/ && !seen[$7]++{printf "%s ", $7}' /tmp/ServerState)
app@user:/tmp> echo $a
FAILED
app@user:/tmp> echo $b
STARTED

Any help on this is really appreciated.

6
  • 1
    I am not able to reproduce this behavior from script. How are you invoking the script? I am invoking as: $ ./script.sh Nov 17, 2014 at 17:43
  • @Ketan I am invoking it as sh script.sh. Just now I tried using ./script.sh and it is giving the desire result. The value of a is FAILED The value of b is STARTED. Thank You so much for the response...Just want to know why it is different while executing the same script using sh file.sh and ./file.sh ??
    – Sudev Jash
    Nov 17, 2014 at 18:10
  • 2
    Suppose sh don't allow command substitution. Try invoke by bash script.sh. Same calling by ./script.sh executes with sha-bang which is /bin/**bash** in your script.
    – Costas
    Nov 17, 2014 at 18:12
  • @SudevJash see: What is the difference between ./ and sh to run a script?
    – muru
    Nov 17, 2014 at 18:18
  • @Costas Yes it is working fine even with bash script.sh and I am getting the desired output... Thank You so much...
    – Sudev Jash
    Nov 17, 2014 at 18:18

2 Answers 2

16

sh (which in most (Debian-derived) systems is linked to dash) doesn't allow process substitution. Try invoke by bash script.sh. Same calling by ./script.sh executes with sha-bang which is /bin/bash in your script.

6
  • @MichaelDurrant Ah, your modification now means the opposite of what I meant to say.
    – muru
    Nov 17, 2014 at 19:29
  • @muru I can't accept modification to bash. I mean dash exactly. file $(which sh) output /bin/sh: symbolic link to 'dash'
    – Costas
    Nov 17, 2014 at 20:43
  • Costas, that's what I meant. Michael Durrant changed my edit to replace dash with bash. Thanks for fixing it.
    – muru
    Nov 17, 2014 at 20:52
  • You mean process substitution (<( echo foo )), not command substitution ($(echo foo)) (which is part of the POSIX specification).
    – chepner
    Nov 18, 2014 at 14:20
  • @chepner You are right. Thank you for comment. Repaired.
    – Costas
    Nov 18, 2014 at 14:22
0

I was trying to invoke the script as sh file.sh and was getting the error. But when i invoked the same script as ./file.sh and bash file.sh it is working and giving the desired result.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .