gpt4 book ai didi

posix - 如何检查标准输入是来自终端还是shell脚本中的管道?

转载 作者:行者123 更新时间:2023-12-03 09:17:25 25 4
gpt4 key购买 nike

我正在编写一个 POSIX shell 脚本,它可能会也可能不会从标准输入接收输入,如 foo.sh < test.txt ,非交互式。

如何检查 stdin 上是否有任何内容,以避免在 while read -r line... 上停止?

最佳答案

如果我答对了问题,您可以尝试以下操作:

#!/bin/sh
if [ -t 0 ]; then
echo running interactivelly
else
while read -r line ; do
echo $line
done
fi

关于posix - 如何检查标准输入是来自终端还是shell脚本中的管道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2456750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com