シェルが人間の行動を検出してから、プロンプトを表示するようにします。
したがって、ファイル名が 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