我希望读取一些数据,可以从管道或命令行参数(比如 $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