加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如果shell以交互模式运行,我该如何检入bash?

发布时间:2020-12-15 18:42:40 所属栏目:安全 来源:网络整理
导读:我怎么能告诉(在?/ .bashrc中)我是以交互模式运行,还是说,通过ssh执行命令.我想避免在.bashrc中打印ANSI转义序列,如果它是后者的话. 根据男子打击: PS1 is set and $- includes i if bash is interactive,allowing a shell script or a startup file to tes
我怎么能告诉(在?/ .bashrc中)我是以交互模式运行,还是说,通过ssh执行命令.我想避免在.bashrc中打印ANSI转义序列,如果它是后者的话.
根据男子打击:

PS1 is set and $- includes i if bash is interactive,allowing
a shell script or a startup file to test this state.

所以你可以使用:

if [[ $- == *i* ]]
then
    do_interactive_stuff
fi

也:

When an interactive shell that is not a login shell is started,bash
reads and executes commands from /etc/bash.bashrc and ~/.bashrc,if
these files exist.

所以?/ .bashrc仅用于交互式shell.有时,人们从?/ .bash_profile或?/ .profile中获取它,这是不正确的,因为它会干扰预期的行为.如果要简化常见代码的维护,则应使用单独的文件来包含公共代码,并从两个rc文件中独立地获取它.

最好是从登录rc文件(例如?/ .bash_profile或?/ .profile)没有输出到stdout,因为它可能会干扰rsync的正常操作.

在任何情况下,测试交互性仍然是一个好主意,因为可能存在不正确的配置.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读