530 likes | 689 Views
Shell. User types command. The OS executes. Shell Interaction with the user and the OS. Shell 的功能在於搭起使用者與作業系統間溝通的橋樑, 提供基本的操作介面,讓使用者得以下達各種指令, 產生彼此的互動關係 可將 Shell 看成一種使用者環境. Shell 簡介. COMMAND. SHELL. 6. 傳回結果. 1. 輸入指令. KERNEL. 5. 解釋訊息. 2. 解釋指令. 硬體. 4. 發出訊息. 3. 控制硬體動作. SHELL -
E N D
Shell User types command The OS executes Shell Interaction with the user and the OS Shell的功能在於搭起使用者與作業系統間溝通的橋樑, 提供基本的操作介面,讓使用者得以下達各種指令, 產生彼此的互動關係 可將Shell看成一種使用者環境
Shell 簡介 COMMAND SHELL 6.傳回結果 1.輸入指令 KERNEL 5.解釋訊息 2.解釋指令 硬體 4. 發出訊息 3.控制硬體動作
SHELL- • 命令與KERNEL之間橋樑 • 命令翻譯器(COMMANDS INTERPRETER) • 分為Bourne-Shell (sh)、C-Shell (csh)、korn-Shell (ksh) 、Small Bourne Shell (ash) 、Bourne Again Shell (bash/bash2) 、Enhanced C Shell (tcsh) 、Zshell (zsh) • Linux 發行版本預設的shell是bash
該使用那個shell • 不同的shell其共同環境參數的設定可能不同 • shell決定了使用者最始的環境 • 使用者可以請系統管理者來設定自己的登入shell,或使用chsh指令來自己更改所使用的shell • chsh 改變/etc/passwd檔案的最後一欄
預設的提示符號 • ash、 ksh 、bash、bash2 、tcsh:$ • zsh:% • 在Fedora Core 3發行版本中bsh、csh、sh等都是符號連結 • bsh → ash • csh → tcsh • sh → bash
Using the C-Shell • C-Shell manual page • man csh • Customize your working environment by using SHELL VARIABLES • Shell Variables • predefined(built-in) variables預定變數 • environment variables環境變數
Shell Variables That Act as Switches: set, unset • To turn on switch variables, usingsetcommand • set [variable-name] set ignoreeof • To turn off switch, using unset command unset ignoreeof • To display shell variables and their current setting, using set (只顯示出已設定的變數名稱)
Built-in Shell Variables:Switches • set ignoreeof • 防止使用[Ctrl-d]來登出系統 • set noclobber • 重導輸出時保護已存在檔案(>, >>無法使用) • ls > file1 • man cat >file1 • man cat >! file1 強制覆蓋檔案,取消保護功能 • set filec • 允許輸入檔案名稱前幾個字 • 若存在一檔案csh.txt • more c<ESC> (相當於csh.txt)
% ls c1c2c3 % echo c? c1c2c3 % echo d? set nonomatch % echo d? % d? unset nonomatch % echo d? % No match nonomatch
Shell Variables That Store Values:set, echo • predefined shell variables that store values along with their uses • Whenever you login, the shell automatically initializes the argv,cwd, home, path, prompt, shell and status variables. • To set a variable of this type, use the set command with the following syntax: set [variable-name=value]
set history = 50 • set prompt = mis% • set prompt =“<I Love UNIX>” • set path =(/etc /bin /usr/bin /usr/local/bin ~ .)
prompt • set prompt=“$user[\!]%” • set prompt=“`hostname`/\!> “ • set prompt =“`hostname`[`pwd`]\!>”
To display Shell Values • set • echo $variable-name • echo $history
Environment Variables: • shell maintains for passing values between programs • these are called ENVIRONMENT VARIABLESor GLOBAL VARIABLES • upper cases names • The value of a global variable is available to any program or shell
Set the Value of Environment Variables • setenv [variabe-name value] • setenv TERM vt100
Display the value of Environment Variables • display all the environment variables printenv setenv • printenv [variable-name] printenv TERM • echo $variable-name echo $TERM • setenv TERM ( set TERM to a null value)
Environment Variable (csh) • Setting the environment (including the following command line in your .login) setenv TERM vt100 mesg n • Listing the environment variables setenv echo $TERM
Environment Variable (csh) • The search path • The pathnames of the search path are separated by space setenv PATH ( /bin /usr/bin /usr/yourlogin/bin . ) • place.tells the shell to search the current directory.
User-defined variables (csh) • User-defined variables • you could place the following line in your .loging to define a variable to hold the pathname. set stuff = /usera/george/bin/stuff • if you also want it to be available to any subshells setenv stuff /usera/george/bin/stuff(.login) or set stuff = /usera/george/bin/stuff(.cshrc)
環境變數與預定變數 • 預定變數用小寫字,環境變數用大寫字 • 定義預定變數用set • 定義環境變數用setenv • 環境變數適用範圍包括定義該變數的shell及在其之下的子shell • 預定變數適用範圍包括定義該變數的shell
設定一預定變數 % set shellvar=melanie % echo $ shellvar 啟動一新shell % csh 再次查詢變數 % echo $ shellvar 變數沒有定義 設定一環境變數 % setenv ENVVAR=yang % echo $ENVVAR 啟動一新shell % csh 再次查詢變數 % echo $ ENVVAR 該變數仍有定義 環境變數與預定變數
How environment and Shell variables are connected • There are six common shell variables that have the same names as environmental variables.(except that environmental variables have uppercase names.) • home, mail, path, shell, term and user
How environment and Shell variables are connected • HOME and SHELL , home and shell • Whenever you log in, Unix automatically sets the values of home and shell(and HOME and SHELL)
How environment and Shell variables are connected • TERM, PATH, USER and term, path, and user • These local variables are tied to the corresponding global variables. • whenever you change the shell variable, the shell will automatically update the global variable. • Within .cshrc and .login files you will set the values of term and path. you will not have to set TERM and PATH because they are updated automatically.
How environment and Shell variables are connected • Home, TERM and USER, and home, term and user, only works in one direction. Changing the environment variable does not change the shell variable. echo $term; echo $TERM set term=hello echo $term; echo $TERM setenv TERM HELLO echo $term; echo $TERM
! • The event number is a value that is increased by 1 every time you enter a new command • To display the event number within a prompt, use an !. Whenever the shell displays the prompt, it will replace the !with the current event number. • You need to precede the ! With a backslash(\). A backslash tells the shell “Interpret the next character literally”; the backslash itself is ignored. • !? 代表前一指令的最後一個參數 • !* 代表前一指令的所有參數
Re-executing History Buffer Commands • !! • !n • !-n • !string • !?string
The History Mechanism (csh) • set history = 10 (.chsrc) • list history list history • repeat the fifth command on the history list ! 5 • repeat last command !!
Startup Files • A startup file contains commands for the shell to execute when it begins running. • Startup files greatly increase the convenience and flexibility of the shell by performing routine tasks such as setting up your terminal, customizing the shell prompt, and reminding you of upcoming events.
初始化script • 設定的環境變數,定義,別名,只在一連線期間有效,一旦登出後,全部會還原為預設值. • 若要永久改變這些值,就要將之放在系統的起始檔案之中 • 每個shell均有其起始檔案,放置在每個使用者的home directory,就像Windows中的AUTOEXEC.BAT • 這檔案在使用者一登入時就被執行,有些SHELL在使用者登出時也會執行某些檔案
login script • 使用者一登入時就執行的起始檔案 • 環境設定的script • 在已登入的shell中執行sub-shell時會執行的檔案,稱為rc script • logout script • 使用者登出時執行的檔案
Login and Shell Initialization Files • A login initialization file • Contain commands that are executed when you first log in.
Login and Shell Initialization Files • Shell initialization file • Contains commands that are executed when a new shell process is started. • This happens when you login and start up a sub-shell.
.與source • .及source可執行一script而不用另外產生一個sub-shell,它們也不需要擁有可執行的權限 • source .login • 以這種方式執行後,在目前的shell中的這些改變會生效,而不需要登出並再次登入,才讓script的改變生效
interactive and noninteractive shell • 每個shell可分成兩部份 • 互動式 • 執行登入的script,rc script • 很多互動式的shell參數不會傳送給非互動式shell • 非互動式 • 當使用者在shell執行script時 ,會呼叫一個非互動式shell • 當一個一個非互動式shell從一個互動式shell執行時,只執行rc script
The C Shell and the .cshrc File • Whenever you start running the C shell, such as when you login in or open a shelltool or cmdtool, the C shell looks for the .cshrc file in your home directory for its initial instructions. • The name is derived from csh, which is the program that uses it. • Setup files ending in rc are read at the time you run the command.
The C Shell and the .cshrc File • The dot at the beginning of the filename indicates that this file is to remain hidden from view when you give the ls command. • A sample .cshrc file is located in /usr/lib/Cshrc(sun os) • omitted out by a pound sign (#)
The C Shell and the .login File • When you log in , after performing instructions in the .cshrc file, the C shell then performs instructions in the .login file. • Subsequent C shells, such as the shelltool or cmdtool windows, ignore the .login file.
The C Shell and the .login File • Use the .login file for initializing remote terminals, starting your windows system(when you first log in to the workstation), and setting up special variables called environment variables. • Unlike shell variables and aliases, environment variables are passed along to subsequentcommands and programs automatically.
The C Shell and the .login File • you need not set them up again every time you start a new C shell or run a new program such as vi. • TERM, PRINTER
.cshrc file and .login file • 當login連線時, UNIX會建立一個shell, 同時會有某些shell variables和environment variables ( such as home and PATH)會被設定 • Shell會在個人目錄中找.cshrc,若存在則執行其內容 • 然後再找.login,並執行其內容 • 只要啟動一個shell時.cshrc就被執行一次
.cshrc file and .login file • .login只有在login連線時執行一次 • 通常將environment設在.login中 • 別名(alias)一般在.cshrc中定義,以便在每次啟動shell時都可以用
C Shell : startup file (.cshrc .login .logout) • shell 會依照下列順序執行 • 全域初始化檔案:/etc/login 或/etc/.login(Solaris)或/etc/csh.login • ~/.cshrc:包含當C shell啟動時所要執行的指令 • ~/.login:只有當使用者登入時才會執行這個檔案