Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University
Objectives Understand different Unix shells and how to change Understand how to use wildcards Understand how to shell variables and how to set up Understand how to create sub-shells Understand common shell commands Understand the difference of using arithmetic expressions in all four shells
Unix Shells 4 Unix command shells: 1.Bourne (by Steve Bourne): provided in most Unix machines, use /bin/sh to create a sub-shell, good for batch processing, lack of aliases and command history, prompt $ 2.C (by Bill Joy): good for interactive work, use /bin/csh to create a sub-shell, has aliases and command history, prompt % 3.Korn (by David Korn): combine interactive features from C shell and programming features from Bourne shell, lack of directory stack manipulation and weak aliases, use /bin/ksh to create a sub-shell, prompt $ 4.Bash(Bourne Again by GNU): most freely available, conform to POSIX shell specification, use /bin/bash to create a sub-shell, prompt bash$ Note: use chsh to change to different login shell.
Linux Command Options There are 3 options are available for Linux commands: 1.Use – (Common used in Unix) 2.Use - - (POSIX form) 3.Use nothing (BSD form) Example: ls –l (common form) ls - - l (POSIX form) ls l (BSD form)
Shell Wildcard NameExplain *0 or more characters ?match any one character [...]match any one of the characters within [ ]
Shell Wildcard Examples: –ls * list all filenames –ls ? list filenames having one character
Shell Variables 1.Environment (or Global) variables: (upper case letter) copied automatically to sub-shell Built-in User-defined 2.Local Variables: used in individual shell only Built-in User-defined
Shell Variables – built-in Environment Built-in Environment Variables NameExplain $HOMEHome directory $PATHSearched path $MAILMailbox path $USERUsername $SHELLLogin shell $TERMTerminal type
Shell Variables – built-in Environment Examples: Check environment variables –env, an external command and runs in a child process –or echo $TERM
Shell Variables – set-up Environment Examples: Change environment variables –set TERM=vt100or –setenv TERM vt100
Shell Variables – built-in Local Built-in Local Variables NameExplain $$Shell PID $0, $1, …, $9 Shell command line parameters $*All shell command line parameters
Shell Variables – built-in Local Examples: Check Local variables –echo $variablename or –set a shell built-in command
Shell Variables – set-up Local Examples: Change Local variables –set variablename=value
Process Control - ps ps: check process status –Syntax: ps –option Option: e: list all currently running processes (A for Linux) f: gives full listing (give ancestry for Linux) l: gives long listing u username: list processes for the user only
Process Control - ps Example: –ps –el list all process information with columns: S: state S: sleeping O: running R: on run queue Z: zombie (parent didn’t wait for death of its child T: stopped or suspended Note: A process is orphan if parent dies before its child
Process Control - ps Example: (Cont.) UID PID: process id PPID: parent PID PRI: priority SZ: total pages in virtual memory TIME: execution time CMD: command
Process Control (Cont.) bg or &: sends foreground process to background Ctrl-z (or ctrl-]-z in DOS): stop process running jobs: shows all background processes id fg %id: sends background process id to foreground kill %id: terminate background process id kill -9 pid: terminate foreground process id Ctrl-c: terminate current running process
Process Control (Cont.) Note: for background process, no standard input is available, no way to see the standard output and standard error. You can either redirect output/error or let kernel to suspend the process right before standard output and standard error by setting as below: stty tostop
Process Control (Cont.) Example: use one Unix window –cat –Ctrl-z –Jobs –cat > file2 & –jobs –fg %1 –Ctrl-z –kill %1 (or kill %cat or kill %?file2) –jobs –tty find the terminal –fg
Process Control (Cont.) Example: (Cont.) use another one Unix window –ps –u username find the process id for the previous shell –kill -9 PID this will terminate the process: cat > file2
Create Sub-Shell 4 ways to create subshells: 1.group shell commands within ( ). Examples: pwd; (cd /; pwd); pwd The commands in ( ) are executed in a subshell. 2.invoke /bin/sh, /bin/csh, /bin/ksh, or /bin/bash 3.starts background processes. 4.starts a shell script
Common Shell Commands command substitution ` ` Examples: echo today is `date`
Common Shell Commands quoting(in pair): Rules: double quotes prevents wildcard replacement only. single quote prevents wildcard replacement, variable substitution and command substitution. only the outer quote works if quotes are nested
Common Shell Commands Examples: echo * The shell expands *. echo "*" The shell does not expands *. echo '*' The shell does not expands *. echo "the user is $USER and files are `ls *`" The shell expands $USER and *. echo 'the user is $USER and files are `ls *`' The shell does not expands either $USER or *.
Common Shell Commands sleep n1 The shell sleeps for n1 seconds. Examples: (sleep 5; echo subshell done)&; ps -f
Common Shell Commands wait [n] The shell suspends its processes until child PID n (background process) terminates. Examples: (sleep 10; echo done #1)&; (sleep 20; echo done #2)&; echo done #3; wait; echo done #4 the shell prints done #3 and wait until subshell prints done #1, them done #2. Then it prints done #4.
Common Shell Commands exit n The shell exits with return code n.
Common Shell Commands eval command It executes the output of the command as a regular shell command. Examples: eval `set echo x=2`; echo $x (Solaris) eval set echo x=2; echo $x (Linux)
Common Shell Commands exec command It executes the command and then terminates the shell. Examples: exec date
Common Shell Commands command1 && command2 command2 will be executed after the success of executing command1 Examples: gcc beep.c && a.out
Common Shell Commands command1 || echo error_message error_message will be printed after the failure of executing command1 Examples: gcc beep_syntaxerror.c || echo compilation error
Difference in Arithmetic Expression C ShellBourne Shell Korn Shell Bash Shell Start Up File.cshrc, then.login.profile.profile or.kshrc.bashrc or.bash_profile Arith metic Expre ssion set x=1 echo x ++ echo $x x=1 echo $x x=`expr $x + 1` echo $x let x=1 echo $x let x=x+1 echo $x declare -i x x=($x+1) echo $x x=($x+1) echo $x
Reference Misc. (for Linux other than Ubantu) killall –u uid: kills all other shell processes killall -9 tcsh: kills all tcsh shell processes Ch. 4