160 likes | 183 Views
C Shell & Unix Environment. C Shell & Unix Environment Ching-Chi Hsu Dept. of Computer Science and Information Engineering. Login-Logout Cycle. startup. init. /etc/ttys. fork (one instance per terminal line). " login: ". exit. getty. user-name. exec. " passwd : ". login.
E N D
C Shell & Unix Environment C Shell & Unix Environment Ching-Chi Hsu Dept. of Computer Science and Information Engineering
Login-Logout Cycle startup init /etc/ttys fork (one instance per terminal line) " login: " exit getty user-name exec " passwd : " login /etc/passwd password exec prompt shell logout command
User Environment .cshrc editors vi , ex or edit login shell csh .exrc .login mail .mailrc .logout child subshell csh .cshrc
csh command interpretation cycle 1.Display the prompt 2.Receive and parse (analyze the syntax of the next command) 3.Perform input processing (history substitutions) 4.Carry or initiate the requested operations • A new process is created (a copy of sell) • The child process is overlaid with the executable file (command name, arguments) • & : return immediately , command run in the background 5.Wait for initiated operations to finish 6.Goto step (1)
Finging the executable file 1. Absolute pathname or relative pathname --- no search is necessary. 2. Simple file name --- searching through an ordered list of directories (command search path). The first such file found is used. Set path = (. ~ ~/bin /usr/local /usr/ucb /usr/bin /bin ) . rehash
Command Execution • Sequential (echo first line ; echo second line) > file • Conditional sequential cmd1 || cmd2 (execute cmd2 only if cmd1 fails) cmd1 && cmd2 (execute cmd2 only if cmd1 success) • background print file& fg jobid [1] running print [2] - stopped cat *.* [3] + stopped cat *.c jobid % job-number %name - prefix % + % -
Input/Output Redirection • Standard Input and Output ------------------------------------------------------------------ File descriptor 0 standard Input keyboard 1 standard output terminal 2 standard error terminal • Redirection ls > temp set noclobber >! cat f1 f2 >> f3 noclobber >>! lisp < file cmd << word (here document)
Input/Output Redirection >& file (standard output and standard error into a new file ) >&! file ~ if file exists >>& file to the end of ~ >>&! file ~ if file does not exist • Pipe ls -l | more ls -l > temp more < temp rm temp cmd1 |& cmd2
Job Control ^Z : stop a running process jobs : display all the background and suspended jobs fg : bg : jobid stop : kill : kill -9 ps kill -9 jobid % jobnumber % name-prefix % + % - [1] stopped vi ... [2] - stopped mail... [3] + running lisp
Job Control %cmd & fg running bg running fg bg bg stopped ^Z stop ^C or complete Kill or complete kill exit %cmd
Input Process • History Substitutions • Alias Substitutions • Variable Substitutions • Command Substitution • File name expansion
Input Process • History Substitution set history = n (length of history list) a event = a command line (whether successful or not) %history -r n (display the history list with sequence number) ! ^xx ^yy !n !* !-n !$ !! !^ !prefix !:n !?string event:s/xx/yy 1. ls -l !-2 = cd 7 cat file1 file2 file3 2. pwd !c = cd :0 :1 :2 :3 3. cd !?s = ls ^ $ 4. Ls : * !! = ls : !7 lpr !7^ !1 = ls -l more !7 : 2-3 lpr !^
Input Process • General form event [: word designator ] [: modifier …] • event event number relative position command prefix matching string • word designator $n, ^, $ , * , $i-j , $I- , $-j , $i* • modifier /usr/src/ucb/prog.c h head !$:h /usr/src/ucb t tail !$:t prog.c r root !$:r /usr/src/ucb/prog e extension !$:e .c p print only
Input Process • Alias Substitution • alias name definition alias dir ls alias ls ‘ls -F’ alias alias name unalias name --- remove alias cd ‘cd \ !* ; pwd ‘ alias csmail ‘mail \!:1%\!:2@relay.cs.net’ cd dir => cd dir ; pwd cs mail user host => mail user% host@relay.cs.net
Input Process • Variable Substitution set maildir = /usr/spool/mail cd $maildir set x = abc echo ${x}def (abcdef) set path = ($home/command /usr/local/bin ...) echo path[2] (2nd argument) • Command Substitution set dir = ‘pwd’ <= 1.execute the command set file = ‘ls’ 2. assign the output to the variable set name = ‘pwd’/test.c
Input Process • File Name Expansion * ? […] , { , , …} ~ . .. • Quote mechanism % echo * % echo ‘*’ % echo \* % echo ‘pwd’ % echo “$home” \ $ * ` “ ‘ ‘ n n n n n n “ y y n y n ` y n n n n