• 交互式shell和非交互式shell、登录shell和非登录shell的区别


    交互式shell和非交互式shell、登录shell和非登录shell的区别。
    首先,这是两个不同的维度来划分的,一个是是否交互式,另一个是是否登录。

    交互式shell和非交互式shell(interactive shell and non-interactive shell)
    交互式模式就是在终端上执行,shell等待你的输入,并且立即执行你提交的命令。这种模式被称作交互式是因为shell与用户进行交互。这种模式也是大多数用户非常熟悉的:登录、执行一些命令、退出。当你退出后,shell也终止了。
    shell也可以运行在另外一种模式:非交互式模式,以shell script(非交互)方式执行。在这种模式 下,shell不与你进行交互,而是读取存放在文件中的命令,并且执行它们。当它读到文件的结尾EOF,shell也就终止了。
    可以通过打印“$-”变量的值(代表着当前shell的选项标志),查看其中的“i”选项(表示interactive shell)来区分交互式与非交互式shell。

     
    yang@mint-linux ~ $ echo $-
    himBH
    yang@mint-linux ~ $ cat test.sh
    echo $-
    yang@mint-linux ~ $ ./test.sh 
    hB
    yang@mint-linux ~ $ 

    登录shell和非登录shell
    登录shell:是需要用户名、密码登录后才能进入的shell(或者通过--login”选项生成的shell)。
    非登录shell:当然就不需要输入用户名和密码即可打开的Shell,例如:直接命令“bash”就是打开一个新的非登录shell,在Gnome或KDE中打开一个“终端”(terminal)窗口程序也是一个非登录shell。
    执行exit命令,退出一个shell(登录或非登录shell);
    执行logout命令,退出登录shell(不能退出非登录shell)。

    yang@mint-linux ~ $ su yang --login
    Password: 
    Hello from .bash_profile
    yang@mint-linux ~ $ exit
    logout
    Hello from .bash_logout
    yang@mint-linux ~ $ su yang --login
    Password: 
    Hello from .bash_profile
    yang@mint-linux ~ $ logout
    Hello from .bash_logout
    yang@mint-linux ~ $ su yang
    Password: 
    Hello from .bashrc
    yang@mint-linux ~ $ exit
    exit
    yang@mint-linux ~ $ su yang
    Password: 
    Hello from .bashrc
    yang@mint-linux ~ $ logout
    bash: logout: not login shell: use `exit'
    yang@mint-linux ~ $ 

    对于Bash来说,登录shell(包括tty1~tty6登录shell和使用“--login”选项的交互shell),它会首先读取和执行/etc/profile全局配置文件中的命令,然后依次查找~/.bash_profile、~/.bash_login 和 ~/.profile这三个配置文件,读取和执行这三个中的第一个存在且可读的文件中命令。
    在非登录shell里,只读取 ~/.bashrc (和 /etc/bash.bashrc、/etc/bashrc )文件,不同的发行版里面可能有所不同。

     其中Ubuntu中~/.profile中包含

    # if running bash
    if [ -n "$BASH_VERSION" ]; then
        # include .bashrc if it exists
        if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
        fi
    fi

    转载:http://smilejay.com/2012/10/interactive-shell-login-shell/

  • 相关阅读:
    【洛谷P6835】线形生物
    【洛谷P2679】子串
    【洛谷P5072】盼君勿忘
    【洛谷P3312】数表
    【洛谷P1447】能量采集
    【洛谷P2257】YY的GCD
    【洛谷P4318】完全平方数
    【AT2300】Snuke Line
    window.showModalDialog
    js typeof
  • 原文地址:https://www.cnblogs.com/yangqionggo/p/3280891.html
Copyright © 2020-2023  润新知