0

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

4

1 回答 1

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
于 2012-07-30T13:40:37.000 回答