测试login是否是scp连接

atx*_*atx 4 linux shell bash scp

我正在回显有关登录的各种机器统计信息,但这对于 SCP 和 SFTP 是有问题的,是否有我可以测试的 shell 变量?

Cak*_*mox 7

在 bash 中,我shopt -q login_shell用来测试它。例如在 .bashrc 中:

if shopt -q login_shell
then
    echo "interesting stuff"
fi
Run Code Online (Sandbox Code Playgroud)

这应该将“有趣的东西”排除在您的 scp/sftp 之外。


Den*_*son 5

根据手册页,您应该测试 中是否存在“i” $-

如果 bash 是交互式的,则设置 PS1 且 $- 包括 i,从而允许 shell 脚本或启动文件测试此状态。

例如:

if [[ $- == *i* ]]
then
    # do interactive stuff
fi
Run Code Online (Sandbox Code Playgroud)