我想让我的shell检测人类行为,然后显示提示。在bash中检查isatty
因此,假设文件名是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
您的示例适用于** stdin **的情况,对于** stdout **,我们可以使用'if [-t 1]'。如果[-t 0] && [-t 1]' – 2012-08-21 01:49:36
。这似乎是一个好主意。有时少就是更多,有时少就是少。所以......这是一个我不知道的shell脚本的新范例,还是您的个人意见?祝你们好运。 – 2012-08-21 01:55:51
感谢您的分享,最好的答案是 – shellter 2012-08-21 02:52:31