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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM