我尝试将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))'
所有括号均已关闭。语法错误在哪里?
答案 0 :(得分:6)
我假设是因为您使用#!/bin/sh
而不是#!/bin/bash
进程替换不可用(或者您有一个不支持进程替换的bash版本,前4 .XX)。切换到bash
或只管道w
到awk
命令:
USERS=$(w | awk '/\/X/ {print $1}')