当stdin来自`cat` bash时,读完全stdin直到EOF

edi*_*999 5 bash stdin cat

我正在尝试将完整的stdin读入变量:

script.sh

#/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 时,它不起作用.

有任何想法吗 ?

Rob*_*ale 6

我会坚持一线

input=$(cat)
Run Code Online (Sandbox Code Playgroud)

当然,Ctrl-D应该用于发出文件结束信号.