Unix Shell Scripting Reference Cheat Crib Sheet
Unix Shell Scripting Reference Cheat Crib Sheet
Brackets
() 1) Groups commands together (See command control). 2) Used in pattern lists (See section on pattern lists) 3) Runs the enclosed command in a subshell. Evaluate and assign value to variable and do math in a shell 1) A range of characters (See section on wildcard expansion). 2) Array subscript. 3) Same as test command. There must be spaces between the brackets and the condition. Conditional expression. Command substitution Evaluate the enclosed expression 1) Parameter substitution. 2) Runs the enclosed command in a subshell.
(())
Initalization les
/etc/profile ${Home}/.profile ${ENV} Contains the system default shell start-up commands. Contains any options and shell functions you want available to your personal operating environment. Contains any options and shell functions you want available everywhere. Common names are $HOME/.kshrc and $HOME/.env Executed each time a child Korn shell is created by a fork(2) system call.
[ ]
Command controls
#!cmd If this the rst line of the script, the script is run under cmd. Usually , cmd is /bin/ksh etc. Otherwise it is a comment. Runs cmd in the background. Runs cmd in the background asynchronously with the parent shell. The parent shell then uses read -p and print -p to communicate with cmd. Command seperator. Runs cmd1 then runs cmd2. Command grouping. Runs cmds in a child shell. Command grouping. Runs cmds in a child shell.
Special Characters
$ | # & ? > < >> << Indicates the beginning of a shell variable name Pipe standard output to the next command 1) Comment character. 2) Also used as #! Executes a process in the background Matches one character Matches one or more characters Output redirection operator Input redirection operator Output redirection operator (to append to a le) Wait until following end-of-input string (HERE operator) space Delimiter between two words \ 1) Escape charater (stops the shell from interpreting the succeeding character as a special character. 2) Line continuation character. + 1) Pattern matching operator 2) Arithmetic operator inside a let command. ; Separates commands if used on the same line. .script.sh Runs the script in the current shell. Equivalent to typing all the lines at the command line.
Variables
Environment Variables set by the Korn shell
$ERRNO Return code from the most recently set system call. $LINENO Line number of the current script (or shell function). $OPTARG Value of the last option argument processed by the getopts command. $OPTIND Index position of the last option argument processed by the getopts command. $PWD Working directory.
...
...
Positional Parameters
$1, $2 ... First, second etc taken from the position of the output.
Flow Control
If
if[ $? -eq 0 ] then grep blob elif [ $? -eq 1 ] then grep blob else grep blob fi
Number Comparison
-eq -ne -ge -le -gt -lt Equal Not equal Greater than or equal too Less than or equal too Greater than Less than
Parameter Substitution
Parameter substitution is signied by { }. The General form is: ${<parameter name>[:<arguments>]} where <>is a substitution and [ ] is optional. ${pattern} ${param} ${#param} ${#identifier[]} ${param:-word} ${param:=word} ${param:+word} Executes pattern in a child shell. Braces are used when a parameter substitution conict may arise i.e the braces are text separators If param is or @, substitutes the number of positional parameters; otherwise, substitutes the length of the value of param. Substitutes the number of elements in the array identier. If param has a value that value is substituted. If not, word is substituted. If param has a value that value is substituted. If not, word is assigned to param and substituted. The exact opposite of :- If param has a value word is substituted. If not, a null is substituted. This can be used to temporarily override a variable. If param has a value that value is substituted. If not, word is printed to stdout and the shell exits. If the shell pattern matches the beginning value of param, the value of this substitution is the value of pattern, with the matched portion deleted. In the rst form, the smallest matching pattern is deleted; in the second, the largest matching pattern is deleted. If the shell pattern matches the ending value of param, the value of this substitution is the value of pattern, with the matched portion deleted. In the rst form, the smallest matching pattern is deleted; in the second, the largest matching pattern is deleted.
File Comparison
le1 -ef le2 Both le1 and le2 exist and refer to the same le le1 -nt le2 le1 is newer then le2. le1 -ot le2 le1 is older then le2.
For
for loop variable in argument list do grep loop variable done
Arrays
Indexing starts at zero.
While
while test true do grep blob done
Until
until test true do grep blob done
Case Statement
case $VARIABLE in match1) commands to execute for 1;; match2) commands to execute for 2;; ) (Optional any other value) commands to execute for no match;; esac break Used to terminate the execution of the entire loop, after completing the execution of all the lines of code up to the break statement. Used to transfer control to the next set of code, but it continues execution of the loop. Exits the script . An integer may be added added to the command, which will be sent as the return code. Used in a function to send data back, or return a result to the calling script
${param%pattern} ${param%%pattern}
Functions
function function name {commands to execute} or function name () {commands to execute}
Pattern Lists
?(pattern-list) Optionally matches anyone of the given patterns. *(pattern-list) Matches zero or more occurrences of the given patterns. +(pattern-list) Matches one or more occurrences of the given patterns. @(pattern-list) Matches exactly one of the given patterns. !(pattern-list) Matches anything except the given patterns.
Comparisons
String Comparison
string=pattern string!=pattern string1 <string2 string1 >sting2 string matches pattern. string does not match pattern. string1 comes before string2 based on the ASCII values for the characters. string1 comes before string2 based on the ASCII values for the characters.
Declaring a shell
#!usr/bin/sh #!usr/bin/ksh #!usr/bin/csh #!usr/bin/bash #!/bin/sh #!/bin/ksh #!/bin/csh #!/bin/bash Bourne Korn C Bourne-Again
Here Document
program name <<LABEL Program Input 1 Program Input 2 LABEL
Exit Signals
noglob
nolog nounset
Mathematical Operators
++ -+ ! * / % == != <= >= < > << >> & ^ | Auto-increment, both prex and postx. Auto-decrement, both prex and postx. Unary plus. Unary minus. Logical negation;binary inversion (ones complement). Multiplication Division. Modulus (remainder) Equality. Inequality. Less than or equal too. Greater than or equal too. Less than. Greater than. Bitwise left shift. Bitwise right shift Bitwise AND. Bitwise exclusive OR. Bitwise OR
0 1 SIGHUP
Normal termination, end of script. Exit Hangup, line disconnected. If nohup was used to execute the script, it would be prevented from hangup. Exit Terminal interrupt, usually CONTROL-C. Core Quit key, child processes to die before terminating Exit Kill -9 command, cannot trap this type of exit status. Core Segmentation Fault Exit Kill commands default action Stop, usually CONTROL-Z.
privileged
trackall verbose
vi viraw xtrace
Inhibits le name expansion. Treats all characters as literal text. The same as set -n. This is useful for debugging Does not save function denitions in the history le. Treats unset parameters as an error, if they are unset during substitution. The same as set -u. Disabled processing of ${Home}/.profile. Uses /etc/suid profile in place of ${ENV}. The same as set -p. Each command becomes a tracked alias when rst encountered. The same as set -h. Prints shell input lines as they are read, but before any substitution has taken place. The lines are sent to stderr with a + at the beginning. The same as set -v. This is useful for debugging Sets inline editor to vi. Process each character as it is typed in vi. Turns on trace mode. The same as set -x. This is useful for debugging. It forces the shell to echo out every command it is executing after it has done variable substitution etc but before it actually runs each command. The command lines are sent to stderr with a + at the beginning.
Typing the command set -o will tell you the options which can be set in the shell. By inputing set -option you are turning a setting on and by using set +option you are turning a setting o. A summary of the most useful options are given. all export Automatically exports all subsequently dened parameters. The same as set -a Runs all background jobs in nice mode. sets in-line editor to emacs. If a command has a non-zero exit status, then executes the ERR trap (signal 7). The same as set -e. sets in-line editor to gmacs. Does not exit shell on Ctrl D. Places all parameter assignments in the current environment. The same as set -k. Marks all directories with a trailing /. Runs background jobs in a separate process group. The same as set -m. Does not allow destruction of existing le contents through shell redirection of output Use >| to override noclobber. Reads commands and checks for syntax erors, but does not execute them. Ignored in interactive shells. The same as set -m.
Intrinsic Functions
abs sqrt exp int cos, sin Absolute value Square root Exponential function Integer part of oating point number cosine, sine etc bgnice emacs errexit
Any user can create a cron table using the crontab -e command. But system administrators can control which users are allowed to create and edit cron tables with the cron.allow and cron.deny. To list the contents of the current users cron table issue the crontab -l. For example: 15 3 8 1 * /usr/bin/somescript.ksh 2>&1 script.log Will execute somescript.ksh on Jan 8 at 03:15 Where * is a weekday (0-6) 0 for Sunday through to 6 for Saturday. If no log le is set the stdout and stderr will be emailed to the user.
Interrupt Handling
When a program is terminated prematurely, we can catch an exit signal. This signal is called a trap. To see the entire list of supported signals for your operating system type: kill -l That is: kill (-ell) The following command will print a message to the screen and make a clean exit on signals 1,2,3 and 15: trap 'echo "\nExiting on a trapped signal" exit;'1 2 3 15
at command
We can use the at command to schedule a job to run at a specic time. System administrators can control which users can schedule jobs with the at.allow and at.dent les.
noexec
Redirects standard output, stdout, to le. Redirects standard input, stdin, from le. Appends redirected standard output, stdout, to le. <> le Opens le for reading and writing as standard input, stdin. << [-] word Here document. Accepts shell input up to an occurrence of word on a line by itself (with no leading spaces or tab stops). the presence of the optional - causes all leading spaces or tab stops in the text to be stripped. Using word causes the text to be read uninterpreted (that is, $var would be copied as $var and not the value of var.) n> le le is the output for le designator n. 0 is standard input, stdin. 1 is standard input, stdout. 2 is standard input, stderr. n>& m Redirects le designator n output to designator m. <& Closes standard input, stdin. >& Closes standard input, stdout. <&p Moves the input from the coprocess to the standard input, stdin. >&p Moves the input from the coprocess to the standard output, stdout. 1>&2 Merges standard output stdout into standard error, stderr. cmd1 |cmd2 Pipes cmd1 standard output stdout, into cmd2 standard input, stdin.
export [name[=value]] Species the list of names to be exported to the environment of child processes. getopts opts string [arg] Shell method for getting command-line arguments into a shell script. !!!Expand!!! kill Kills a process letarg ((arg)) Korn shell expression capability. Arithmetic evaluation is done in the same manner as the expr print [-Rnprsu[n]] [arg] Shell output mechanism. Prints text as specied. This command replaces echo. read [-prsu[n]] options Shell input mechanism. Reads text as specied. readonly [name[=value]] Sets specied variables once at read time; cannot be changed. return [n] Immediately terminates the function. Default is a 0 return code; otherwise, n can be set from 0 through to 255. POSIX uses a range of 0 through to 125. set [+options] arg Sets options for the shell. Most options correspond to shell ags. 1) No args 2) Reassign positional parameters. 3) set-o (see section on set -o options) shift [n] ?????
typeset args [name[=value]] Set the attributes and values for shell parameters and variables. ulimit [-afu] [n] Imposes a size limit of n blocks. -f species that n equals 4096 blocks for each child process. -a displays current settings. -u ? umask [mask] User le creation mask. The default le permissions. Common masks are 077, 027, 022. unalias name Deletes name as an alias. unset [-f] name Deletes name as an alias. whence [-pv] name For each name specied, identies how it will be used. -v produces a verbose output. -p does a path search for name. wait [PID] Wait until child process has nished. If no option is set the script will wait until all child processes have nished.
Downcasing
downcasevar=$(echo $variable | tr [A-Z] [a-z]) or typeset -l variable
sleep time Time for shell script to be inactive in seconds. ssh ????? test expression Tests expression and gives a return code of in the $? variable. A return code of 0 is true anything else is false. times Prints the accumulated system and user times for this shell and child processes. time ????? timex ????? trap args sigs Breaks execution in the shell and executes args