我想我了解交互式、登录和批处理 shell 之间的区别。请参阅以下链接以获取更多帮助:
我的问题是,如果我在交互式、登录或批处理 shell 上,如何使用命令/条件进行测试?
我正在寻找一个命令或条件(返回true或false),我也可以放在 if 语句中。例如:
if [[ condition ]]
echo "This is a login shell"
fi
Run Code Online (Sandbox Code Playgroud)
Chr*_*own 216
我假设有一个bashshell 或类似的,因为标签中没有列出 shell。
[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive'
Run Code Online (Sandbox Code Playgroud)
shopt -q login_shell && echo 'Login shell' || echo 'Not login shell'
Run Code Online (Sandbox Code Playgroud)
通过“批处理”,我假设您的意思是“非交互式”,因此检查交互式 shell 就足够了。
Gil*_*il' 48
在任何 Bourne 风格的 shell 中,该i选项指示 shell 是否是交互式的:
case $- in
*i*) echo "This shell is interactive";;
*) echo "This is a script";;
esac
Run Code Online (Sandbox Code Playgroud)
没有可移植且完全可靠的方法来测试登录 shell。Ksh 和 zsh 添加l到$-. Bash 设置login_shell选项,您可以使用shopt -q login_shell. 可移植地,测试是否$0以-:开头,shell 通常知道它们是登录 shell,因为调用者-向参数零添加了前缀(通常是可执行文件的名称或路径)。这无法检测到特定于 shell 的调用登录 shell 的方式(例如ash -l)。
ohs*_*ite 25
这是fish其他用户偶然发现此页面时的答案。
if status --is-interactive
# ...
end
if status --is-login
# ...
end
echo "darn, I really wanted to have to use globs or at least a case statement"
Run Code Online (Sandbox Code Playgroud)
小智 21
对于csh和tcsh我已经在我的以下.cshrc文件:
if($?prompt) then # Only interactive shells set $prompt
...
endif
Run Code Online (Sandbox Code Playgroud)
专门为tcsh,该变量loginsh是为登录 shell 设置的:
if($?loginsh) then # A login shell..
...
endif
Run Code Online (Sandbox Code Playgroud)
(tcsh还有一个变量shlvl设置为嵌套 shell 的数量,其中登录 shell 的值为 1。)
Adr*_*ish 17
另一种方法是检查结果 tty
if [ "`tty`" != "not a tty" ]; then
Run Code Online (Sandbox Code Playgroud)
小智 14
UNIX/Linux 有一个命令来检查您是否在终端上。
if tty -s
then
echo Terminal
else
echo Not on a terminal
fi
Run Code Online (Sandbox Code Playgroud)
Ang*_*elo 13
您可以检查 stdin 是否为终端:
if [ -t 0 ]
then
echo "Hit enter"
read ans
fi
Run Code Online (Sandbox Code Playgroud)
小智 11
对于 Zsh
# Checking Interactive v.s. Non-Interactive
[[ -o interactive ]] && echo "Interactive" || echo "Non-Interactive"
#
# Checking Login v.s. Non-Login
[[ -o login ]] && echo "Login" || echo "Non-Login"
Run Code Online (Sandbox Code Playgroud)
i不是要寻找的正确选项。-i是强制非交互式 shell 变为交互式 shell。正确的自动启用选项是-s,但不幸的是 Bash 没有正确处理这个问题。
您需要检查是否$-包含s(这被授予自动激活)或是否包含i(这未被授予自动激活,但正式仅耦合到-ishell 的命令行选项)。