交互式shell和非交互式shell、登陸shell和非登陸shell的區別

交互式shell和非交互式shell、登陸shell和非登陸shell的區別。
首先,這是兩個不一樣的維度來劃分的,一個是是否交互式,另外一個是是否登陸。linux

交互式shell和非交互式shell(interactive shell and non-interactive shell)
交互式模式就是在終端上執行,shell等待你的輸入,而且當即執行你提交的命令。這種模式被稱做交互式是由於shell與用戶進行交互。這種模式也是大多數用戶很是熟悉的:登陸、執行一些命令、退出。當你退出後,shell也終止了。
shell也能夠運行在另一種模式:非交互式模式,以shell script(非交互)方式執行。在這種模式 下,shell不與你進行交互,而是讀取存放在文件中的命令,而且執行它們。當它讀到文件的結尾EOF,shell也就終止了。
能夠經過打印「$-」變量的值(表明着當前shell的選項標誌),查看其中的「i」選項(表示interactive shell)來區分交互式與非交互式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)。
bash

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 )文件,不一樣的發行版裏面可能有所不一樣。post

 其中Ubuntu中~/.profile中包含spa

# 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/code

相關文章
相關標籤/搜索