awk:意外令牌附近的语法错误`(''

时间:2013-10-27 11:27:34

标签: bash awk

我尝试将awk命令的输出分配给变量:

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

此行是以下脚本的一部分:

#!/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

但是,我收到以下错误:

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

所有括号均已关闭。语法错误在哪里?

1 个答案:

答案 0 :(得分:6)

我假设是因为您使用#!/bin/sh而不是#!/bin/bash进程替换不可用(或者您有一个不支持进程替换的bash版本,前4 .XX)。切换到bash或只管道wawk命令:

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