0

I have the following script code:

test.sh

echo "BEGIN"
while read CMD <&1; do
    [ -z "$CMD" ] && continue
    case "$CMD" in
    start)
            echo "get_start"
            ;;
    stop)
            echo "get_stop"
            ;;
    *)
            echo "get_uknown_command"
            ;;
    esac
    echo "END";
done

When I run it with:

$./test.sh <input.txt

I get my script locked

input.txt

start
stop
sthh

Why my script is locked? How I can fix that?

BTW: If I enter the data manually then the script will not lock.

3
  • 2
    Don't read from stdout (&1). Try &0. Commented Jul 12, 2013 at 11:01
  • @ott yes indded that was the error in the script. &0 means stdin and &1 means stdout. thnak you for the comment. make it an answer and I will accept it Commented Jul 12, 2013 at 11:03
  • 3
    Why the redirect anyway? Just while read CMD; do should work. Commented Jul 12, 2013 at 11:12

1 Answer 1

4

Your second line is incorrect, and overly complex anyway. The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively, so to read from stdin you'd want to have

while read CMD <&0; do 

However, since stdin is the default input for read,

while read CMD; do

is really the simplest way to go. This way, you can manually enter the commands, or use redirection on the command line to read from a file.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.