当前位置: 动力学知识库 > 问答 > 编程问答 >

linux - Using a file input as stdin for a shell script does not work

问题描述:

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.

网友答案:

What do you need the <&1 for? Remove it, and it works.

while read CMD; do

./test.sh  < input.txt 
BEGIN
get_start
END
get_stop
END
get_uknown_command
END
分享给朋友:
您可能感兴趣的文章:
随机阅读: