尝试在bash中读取fifo。为什么我会收到错误?
pipe="./$1"
trap "rm -f $pipe" EXIT
if [[ ! -p $pipe ]]; then
mkfifo $pipe
fi
while true
do
if read line <$pipe; then
if "$line" == 'exit' || "$line" == 'EXIT' ; then
break
elif ["$line" == 'yes']; then
echo "YES"
else
echo $line
fi
fi
done
echo "Reader exiting"
我得到的错误:
./sh.sh: line 12: [: missing `]' ./sh.sh: line 14: [HelloWorld: command not found
从其他shell运行并打印HelloWorld
答案 0 :(得分:2)
您缺少if
语句的命令,而您需要
elif
语句中的空格。
if "$line" == 'exit' || "$line" == 'EXIT' ; then
break
elif ["$line" == 'yes']; then
应该是
if [ "$line" = 'exit' ] || [ "$line" = 'EXIT' ] ; then
break
elif [ "$line" = 'yes' ]; then
如果你不介意bashisms,那就是一个稍微清洁一点的选择:
if [[ $line = exit || $line = EXIT ]]; then
break
elif [[ $line = yes ]]; then