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

时间:2014-04-28 00:05:33   收藏:0   阅读:210362

交互式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 `exityang@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/

交互式shell和非交互式shell、登录shell和非登录shell的区别,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!