【发布时间】:2015-04-07 21:42:29
【问题描述】:
我想从管道或命令行参数(比如$1)读取一些数据,以提供的为准(优先级有管道)。
这个片段告诉我管道是否打开,但我不知道在里面放什么以免阻塞脚本(test.sh)(使用read或cat)
if [ -t 0 ]
then
echo nopipe
DATA=$1
else
echo pipe
# what here?
# read from pipe into $DATA
fi
echo $DATA
执行上面的test.sh 脚本我应该得到以下输出:
$ echo 1234 | test.sh
1234
$ test.sh 123
123
$ echo 1234 | test.sh 123
1234
【问题讨论】: