4

我试图将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))'

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

4

1 回答 1

6

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

USERS=$(w | awk '/\/X/ {print $1}')
于 2013-10-27T11:52:15.943 回答