bash - 在 bash 中检查 isatty

标签 bash shell

我想让我的 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/

相关文章:

bash - 为什么我的 bash 脚本因 ffmpeg 而停止?

linux - if thens 的 Bash 语法

bash - 如何使用 sed 删除特定字符后面的数字?

git hook 来测试文件是否包含正确的数据

linux - 为什么在非交互式 bash session 中运行 gradlew 会关闭 session 的标准输入?

c - 如何在 shell 中运行 C 函数?

c - 为什么 execvp 或 exec* 系列函数之后的任何内容都不会被执行?

regex - Bash:测试一个变量是否存在于另一个变量中?

linux - 如何在 Linux 中查找所有 .zip 文件和文件大小

shell - 如何在 Unix 上的 Makefile 中获取文件大小?