我想让我的 shell 检测是否有人类行为,然后显示提示。
因此,假设文件名为 test.bash
#!/bin/bash
if [ "x" != "${PS1:-x}" ] ;then
read -p "remove test.log Yes/No" x
[ "$x" = "n" ] && exit 1
fi
rm -f test.log
但是,我发现如果我没有设置 PS1 是不能工作的。有没有更好的方法?
我的测试方法:
./test.bash # human interactive
./test.bash > /tmp/test.log # stdout in batch mode
ls | ./test.bash # stdin in batch mode
最佳答案
详细说明,我会尝试
if [ -t 0 ] ; then
# this shell has a std-input, so we're not in batch mode
.....
else
# we're in batch mode
....
fi
希望对您有所帮助。
关于bash - 在 bash 中检查 isatty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10022323/