V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
atom
V2EX  ›  Linux

如何判断终端类型是login还是non-login

  •  
  •   atom · 2013-03-05 14:01:20 +08:00 · 3876 次点击
    这是一个创建于 4083 天前的主题,其中的信息可能已经有所发展或是发生改变。
    写了一个脚本,需要在里面判断,当前运行的终端是 login-shell 还是 non-login shell。
    man了半天无头绪,求教!
    3 条回复    1970-01-01 08:00:00 +08:00
    BOYPT
        1
    BOYPT  
       2013-03-05 14:45:50 +08:00   ❤️ 1
    non-login shell你指cronjob里面跑的那种么?

    可以 [[ -t 0 ]] 判断stdin是否打开,没打开就是非交互shell。
    atom
        2
    atom  
    OP
       2013-03-05 15:56:49 +08:00
    @BOYPT
    看到你的回答我重翻了下abs,-t这个选项好冷门啊

    cronjob属于哪种shell我一直不晓得,它不会读取任何配置文件。

    non-login shell比较典型的代表就是
    ssh host 'command'
    衍生的scp
    以及su username
    BOYPT
        3
    BOYPT  
       2013-03-05 16:23:06 +08:00
    @atom 额,我就man bash里面查的...

    $ ssh [email protected] "[[ -t 0 ]] && echo stdin is a tty"
    [email protected]'s password:

    没返回,那看起来这样判断应该可以的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3253 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:42 · PVG 20:42 · LAX 05:42 · JFK 08:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.