introduction to unix (2)
DESCRIPTION
Introduction to UNIX (2). The UNIX Shells Shell functionality Selecting a shell Redirection/Substitution/Sequences Scripts Variables Process control Core built-in commands. Shell Functionality. A shell is an interface to the raw OS - PowerPoint PPT PresentationTRANSCRIPT
Introduction to UNIX (2)
• The UNIX Shells – Shell functionality– Selecting a shell– Redirection/Substitution/Sequences– Scripts– Variables– Process control– Core built-in commands
Shell Functionality
• A shell is an interface to the raw OS– Built-in commands, scripts, variables, pipe, bac
kground job, wildcards, subshells, I/O redirection, substitution, sequences, ...
– Selecting a shell• Bourne shell: $ prompt, /bin/sh• Korn shell: $ prompt, /bin/ksh• C shell: % prompt, /bin/csh
– Command for changing a shell: chsh – Checking your current shell: echo $SHELL
Shell Operations
• Shell operation when you log-in– Reads a special startup file located in your home dir that c
ontains some initialization information• .cshrc file (C shell), .profile (Bourne & Korn shell)
– Displays a prompt and waits for a user command• A command longer than a line is possible using \
– If user types a Control-D or some exit command, the shell is terminated
• Command: executable files vs. shell commands– e.g. ls is an executable located in /bin/ls– e.g, cd or echo are shell built-in commands
Meta Characters• Some characters specially processed
> : Output redirection; write standard output to a file>> : Appends standard output to a file< : Input redirection; read standard input from a file* : File substitution wildcard; matches 0 or more chars? : File substitution wildcard; matches any single char[..]: File substitution wildcard; matches any char in [..]| : Pipe; send a process output to input of another one; : Used for sequencing commands$ : Expands the value of a variable`command`: Command substitution; replaced by output
More Meta Characters|| : Conditional execution; execute if prev. one fails&& : Conditional execution; execute if prev. one succeed(..) : Groups commands& : Run a command in the background# : For comment use\ : Prevents special interpretation of the next character– When shell scans given command, it first processes all meta characters in it specially, then it
executes the command% echo hi > file% echo hi > file% cat file% cat filehihi% echo hi \> file% echo hi \> file% cat file% cat filehi > filehi > file
%_%_
I/O Redirection (>, >>, <)• A process has 3 standard I/O/E channels
– Standard input, output, error– By default, they are keyboard, screen, screen
• Output redirection – Store standard output of a process to a file
command > filename or command >> filename
– Store standard error of a process to a filecommand 2> filename
• Input redirection– Use a file as standard input of a process
command < filename or command << filename
WildCards (*, ?, [..]) % ls a.c b.c cc.c unix1.ppt unix2.ppt % ls *.ppt unix1.ppt unix2.ppt % ls ?.c a.c b.c % ls [ac]* a.c cc.c % ls [A-Za-z]* a.c b.c cc.c unix1.ppt unix2.ppt
Pipes (|)% ls a.c b.c cc.c unix1.ppt unix2.ppt% ls | wc -w 5% ls | tee ls.capture | wc -w 5% cat ls.capture a.c b.c cc.c unix1.ppt unix2.ppt
Command Substitution (`)• A command surrounded by ` is executed and its standrad ou
tput is inserted in the command’s place
% echo the date today is `date` the date today is Mon May 30 13:40:55 PST 2000% who smoon ttyp0 May 30 13:00 (masslab:0.0) jaemok ttyp1 May 30 11:00 (maia)% echo there are `who | wc -l` users on the system there are 2 users on the system
Command Sequences (;)• Commands separated by ; will be executed in sequence
% date; ls ; pwd Mon May 30 13:40:55 PST 2000 a.c b.c cc.c unix1.ppt unix2.ppt /home/smoon% date > date.txt ; ls ; pwd > pwd.txt a.c b.c cc.c date.txt unix1.ppt unix2.ppt
Conditional Sequences (&&, ||)
• Every UNIX process has an exit value– Zero (0) means a success while non-zero (1) means a failure
• A && B: B is executed only if A returns zero• A || B: B is executed only if A returns non-zero
% cc myprog.c && a.out% cc myprog.c || echo compilation failed
Grouping Commands ()• Commands grouped with () are executed by a child shell whi
ch shares the same standard I/O/E channels
% date; ls ; pwd > out.txt Mon May 30 13:40:55 PST 2000 a.c b.c cc.c unix1.ppt unix2.ppt% cat out.txt /home/smoon % (date ; ls ; pwd) > out.txt
Background Processing (&)• A command ended with & is executed by a child shell as a backgro
und process which runs concurrently with the parent shell and does not take conrol of keyboard– Useful for concurrent tasks (but windowing obviated it)% find . -name a.c -print ./wild/a.c ./reverse.tmp/a.c% find . -name a.c -print & 27174% pwd ./wild/a.c /home/smoon % ./reverse.tmp/a.c% find . -name a.c -print > find.txt &
Shell Programs: Scripts• A series of shell commands saved in a file for later exec.
– The file should have a execute permission (using chmod) – To run the script, simply type the file name as regular command– The first line determines which shell to use for execution
• If it is just #, then the same shell you executed the script• If it is #! pathName, then pathName shell, otherwise Bourne shell
% cat > script.csh #!/bin/csh echo -n the date today is # -n in csh omits new line date exit 0 ^D% chmod +x script.csh% script.cshthe date today is Mon May 30 13:40:55 PST 2000
Subshells• Your parent shell may creates a subshell for some tasks
– When executing a grouped shell or a script; parent shell sleeps– When executing a background job, parent shell continues exec.
• Subshell’s working dir does not affect that of parent’s % pwd /home/smoon% (cd / ; pwd) /% pwd/home/smoon
• Each shell has two kinds data spaces– Environment space and local-variable space– A child shell inherits parent’s environment space and a clean local-variable sp
ace
Environment
Local Environment
Local
Parent Shell
Child Shell
Variables
• Environment variable & local variable– Hold data in a string format– Every shell has predefined environment vars
$Home: full path name of your home directory$PATH: list of directories to search for commands$MAIL: full path name of your mailbox$USER: your user name$SHELL: full path name of your login shell$TERM: type of your terminal
% echo HOME = $HOME, PATH = $PATHHOME = /home/smoon, PATH = /bin:/usr/bin:/usr/sbin
– Other environment vars and local vars may be created as needed (useful for writing scripts)
Environment vs Local Vars• Bourne shell example
$ firstname=Soo-Mook$ lastname=Moon$ echo $firstname $lastnameSoo-Mook Moon$ export lastname$ sh .. Start a child shell; parent sleeps$ echo $firstname $lastnameMoon$ ^D .. Terminate child; parent awakens$ echo $firstname $lastname Soo-Mook Moon$ _
Special Built-In Variables$$: Process ID of the shell, $0: Name of the shell script$1..$9: $n is n-th command line arguments, $*: List of all
$ cat script.shecho the name of this script is $0echo the first argument is $1echo a list all arguments is $*echo this script places the date into a temporary file called $1.$$date > $1.$$ls $1.$$rm $1.$$$ script.sh a b cecho the name of this script is script.shecho the first argument is aecho a list all arguments is a b cthis script places the date into a temporary file called a.32751a.32751
Quoting• Inhibit wildcards, variable-substitution, command -sub
stitution using ‘ (all) or “ (wildcards only)
$ echo 3 * 4 = 123 a.c b.c cc.c 4 = 12$ echo “3 * 4 = 12”3 * 4 = 12$ echo ‘my name is $name - date is `date`’my name is $name - date is `date`$ echo “my name is $name - date is `date`” my name is Moon - date is Mon May 30 13:40:55 PST 2000
Here Documents (<<)• % command << word
– Copies its standard input up to, but not including the line starting with word into a shell buffer, then execute command using the contents of the buffer as input
$ cat here.shmail $1 << ENDOFTEXTDear $1,Hi!- $USERENDOFTEXTecho mail sent to $1$ here.sh smoon mail sent to smoon$_
Job Control• Obtain a listing of your current processes and contol t
heir behavior - ps, kill, wait, sleep, ..
$ (sleep 10; echo done) & 27387
$ ps PID TTY TIME CMD27355 pts/3 0:00 -sh ___ the login shell27387 pts/3 0:00 -sh ___ the subshell27388 pts/3 0:00 sleep 10 ___ the sleep27389 pts/3 0:00 ps ___ the ps command itself$ done
PS Meaning• S: process status
– (R: runnable, S: sleeping, T: suspended, Z: zombie)
• UID: effective user ID of the process• PID: ID of the process• PPID: ID of the parent process• TIME: amount of CPU time used• CMD: command• C: percentage of CPU time used by the process• PRI: priority of the process• SZ: size of the process in KB• TTY: the controlling terminal
Signalling Processes: Kill• Wish to terminate a process before it completes
– kill [-signalID] {pid}+ :sends a signal with code signalID to numbered process (by default, a TERM signal, but if it won’t work, send a KILL (-9) signal)
$ (sleep 10; echo done) & 27387
$ kill 27387$ psPID TTY TIME CMD27355 pts/3 0:00 -sh ___ the login shell27389 pts/3 0:00 ps ___ the ps command itself$ kill -l HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM U
RG STOP TSTP CONT CHLD TTIN TTOU IO XCPU XFSZ VTALRM PROF WINCH LOST USR1 USR2
Wait for Child Process: wait
• A process may wait for one or more of its child processes to terminate by executing wait– Useful for advanced scripts
$ (sleep 30; echo done 1) &
27387$ (sleep 30; echo done 2) &27390$ echo done 3 ; wait echo done 4done 3done 1done 2done 4$
Finding a Command: $PATH
• When a shell processes a command, it checks– If the command is a built-in; if so, executes it directly$ echo done 1done 1
– If not, checks if it starts with /; if so, it is the absolute pathname of the command, so execute the program
$ /bin/lsa.c b.c cc.c
– If not, the shell searches directories whose names are stored in PATH environment variable, from left to right
• If there is a match for the command, then execute the file• Otherwise (or if PATH is empty & it is not in pwd), an error
$ echo $PATH /bin:/usr/bin:/usr/sbin
$ lsa.c b.c cc.c$ foofoo: not found
Overload Standard Utilities• Users often creates a bin subdirectory in their home and pl
ace it before /bin in their PATH setting– Can overload standard utilities with their own ones$ mkdir bin$ cd bin$ cat > lsecho my ls^D$ chmod +x ls$ echo $PATH/bin:/usr/bin:/usr/sbin$ echo $HOME/home/smoon$ PATH=/home/smoon/bin:$PATH$ lsmy ls
• Only this shell (and its subshells) is affected by this change of the PATH variable
Other Built-in Commands• eval command
– Executes the output of command; useful for processing the output of utilities that generate shell commands (e.g., tset)
$ echo x=5x=5$ eval `echo x=5`$ echo $x5
• exec command– Causes shell’s image to be replaced by command; if command i
s successful, the shell who executes exec dies$ exec dateMon May 30 13:50 PST 2000login:
Other Built-in Commands• umask [octal value]
– When shell performs >, it creates a file with O666 permission but it is exclusive-or with umask setting for final permission
$ date > date.txt$ ls -l date.txt-rw-r--r-- 1 smoon 10 May 30 13:50 date.txt$ umask22$ umask 0$ date > date.txt$ ls -l date.txt-rw-rw-rw- 1 smoon 10 May 30 13:50 date.txt
• Default umask value is O22, but you can change it with umask command, which is usually done in the shell start file (e.g., .cshrc)