我正在尝试将完整的stdin读入变量:
#/bin/bash
input=""
while read line
do
echo "$line"
input="$input""\n""$line"
done < /dev/stdin
echo "$input" > /tmp/test
Run Code Online (Sandbox Code Playgroud)
当我运行ls | ./script.sh或大多数其他命令时,它工作正常.
但是,当我运行cat | ./script.sh,输入我的消息,然后按Ctrl-C退出cat 时,它不起作用.
有任何想法吗 ?