5

I tried to assign the output of an awk command to a variable:

USERS=$(awk '/\/X/ {print $1}' <(w))

This line is part of the following script:

#!/bin/sh

INTERFACE=$1 # The interface which is brought up or down
STATUS=$2 # The new state of the interface

case "$STATUS" in
    up) # $INTERFACE is up

        if pidof dropbox; then
          killall dropbox
        fi

        USERS=$(awk '/\/X/ {print $1}' <(w))

        for user in $USERS; do
            su -c "DISPLAY=$(awk '/\/X/ {print $11}' <(w)) dropboxd &" $user
        done
        ;;
    down) # $INTERFACE is down
        ;;
esac

However, I get the following error:

script: command substitution: line 14: syntax error near unexpected token `('
script: command substitution: line 14: `awk '/\/X/ {print $1}' <(w))'

All brackets are closed. Where is the syntax error?

orschiro
  • 19,847
  • 19
  • 64
  • 95
  • 4
    What is this subexpression meant to be: `<(w)` ? – piokuc Oct 27 '13 at 11:30
  • "line 14"? It looks like the syntax error may be introduced earlier in your script. – johnsyweb Oct 27 '13 at 11:31
  • 3
    @piokuc: Try `cat <(w)` to see how [`<()` process substitution](http://tldp.org/LDP/abs/html/process-sub.html) works. – johnsyweb Oct 27 '13 at 11:32
  • @piokuc When executed in a shell, it shows me the username of the currently logged in user of the running X session: `~ $ awk '/\/X/ {print $1}' <(w) orschiro` – orschiro Oct 27 '13 at 11:35

1 Answers1

7

I'm assuming because you are using #!/bin/sh and not #!/bin/bash that process substitution is not available (or you have a version of bash that doesn't support process subsitiution, pre 4.X.X). Switch to bash or just pipe w to your awk command:

USERS=$(w | awk '/\/X/ {print $1}')
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202