0x90
0x90

Reputation: 40982

Why do I get an error while reading fifo in shell?

Trying to read fifo in bash. why do I get error?

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"

The error I get:

./sh.sh: line 12: [: missing `]' ./sh.sh: line 14: [HelloWorld: command not found

When running from other shell and print HelloWorld

Upvotes: 0

Views: 209

Answers (1)

chepner
chepner

Reputation: 531165

You are missing a command for the if statement, and you need spaces in the elif statement.

if "$line" == 'exit'  || "$line" == 'EXIT' ; then
    break       
elif ["$line" == 'yes']; then

should be

if [ "$line" = 'exit' ] || [ "$line" = 'EXIT' ] ; then
    break       
elif [ "$line" = 'yes' ]; then

A slightly cleaner option, if you don't mind bashisms:

if [[ $line = exit || $line = EXIT ]]; then
    break
elif [[ $line = yes ]]; then

Upvotes: 2

Related Questions