小编Tom*_*Tom的帖子

bash脚本读取管道或参数

我希望我的脚本从stdin读取字符串,如果是管道,或者从参数读取.所以首先我想检查一些文本是否是管道,如果不是,它应该使用一个参数作为输入.我的代码看起来像这样:

value=$(cat) # read from stdin
if [ "$pipe" != "" ]; then #check if pipe is not empty
 #Do something with pipe string
else
 #Do something with argument string
fi
Run Code Online (Sandbox Code Playgroud)

问题是当它没有管道时,脚本将停止并等待"ctrl d",我不想要那个.关于如何解决这个问题的任何建议?

提前致谢./托马斯

linux bash stdin pipe

6
推荐指数
3
解决办法
4677
查看次数

标签 统计

bash ×1

linux ×1

pipe ×1

stdin ×1