【问题标题】:Using a file input as stdin for a shell script does not work使用文件输入作为 shell 脚本的标准输入不起作用
【发布时间】:2013-07-10 21:16:08
【问题描述】:

我有以下脚本代码:

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

当我运行它时:

$./test.sh <input.txt

我的脚本被锁定

input.txt

start
stop
sthh

为什么我的脚本被锁定了?我该如何解决?

顺便说一句:如果我手动输入数据,那么脚本将不会锁定。

【问题讨论】:

    标签: linux file shell ubuntu input


    【解决方案1】:

    您需要&lt;&amp;1 做什么? 删除它,它就可以工作了。

    while read CMD; do

    ./test.sh  < input.txt 
    BEGIN
    get_start
    END
    get_stop
    END
    get_uknown_command
    END
    

    【讨论】:

    • 它有效。谢谢。解释为什么它不适用于 1?
    • 我在使用这段代码时不小心。 &lt;&amp;1 表示从标准输出获取。所以我的脚本期待来自标准输出的输入数据而不是标准输入。用&lt;&amp;0 改变它就可以了。
    猜你喜欢
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 2015-06-26
    • 1970-01-01
    相关资源
    最近更新 更多