为什么我在shell中读取fifo时会出错?

时间:2012-07-30 13:18:20

标签: linux bash pipe

尝试在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

1 个答案:

答案 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