Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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.

share|improve this question
2  
Don't read from stdout (&1). Try &0. –  ott-- Jul 12 '13 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 –  MOHAMED Jul 12 '13 at 11:03
3  
Why the redirect anyway? Just while read CMD; do should work. –  manatwork Jul 12 '13 at 11:12
    
@manatwork this works too. Thank you –  MOHAMED Jul 12 '13 at 11:14

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.