0% found this document useful (0 votes)
770 views

Unix Shell Scripting Reference Cheat Crib Sheet

This document provides a reference sheet for Unix Korn Shell scripting. It summarizes key concepts like brackets, command controls, environment variables, initialization files, file name generation, variables, flow control, conditional logic, functions, and pattern lists. It also describes common operators, commands, and built-in variables used in Korn shell scripting.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
770 views

Unix Shell Scripting Reference Cheat Crib Sheet

This document provides a reference sheet for Unix Korn Shell scripting. It summarizes key concepts like brackets, command controls, environment variables, initialization files, file name generation, variables, flow control, conditional logic, functions, and pattern lists. It also describes common operators, commands, and built-in variables used in Korn shell scripting.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Unix Korn Shell Scripting Reference Sheet

Remember: Unix is case sensitive.

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.

Environment Variables used by the Korn shell


Search path for the cd built-in command. Number of columns in the users terminal display. Path name to the script executed when the shell is invoked. $FPATH Search path for function denitions. $HOME Set to the name of your home directory. $IFS Internal eld seperator. The default IFS is set to a space, tab stop and carriage return. $LINES Number of lines in the users terminal display. $MAIL Name of the mail le to check for incoming electronic mail. $MAILPATH List of the mail les to check for incoming electronic mail. This variable overrides the $MAIL variable $MAILCHECK Species how often your mailbox is checked for new mail. The default value is 600 (seconds). $PS1 Primary prompt string, $ by default. You can change this to whatever you want. $PS2 Secondary prompt, >by default. This is the prompt you will see if you enter a command that requires more input than you have provided. This could happen if, for example, you continue a line or forget to close quotes. $PS3 Primary prompt string for the select loop. Initially set to # ?. $PS4 Trace mode line identier. Initially set to +. $PATH List of directories to search for an executable command. $SHELL The current unix shell in session. $TERM The type of terminal you are using. $USER Username of user. $CDPATH $COLUMNS $ENV

(())

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.

cmd & cmd |&

cmd1 ;cmd2 (cmds ) {cmds ;}

File Name Generation


Wildcard Expansion
* ? [chl] [n-z] [a,z] [az] Matches Matches Matches Matches Matches Matches any string including null string. any single character. any of the enclosed characters chl. characters n through to z. characters a or z. characters a or z.

Built-in variables or automatic parameters


$0 Set to the name of the currently executing script. Often used in error messages to identify the script in which the error occurred. $# Set to the number of arguments passed to the script. Usually tested at the beginning of a script to check that the user has passed the correct number of arguments. $ Contains a list of all the arguments passed to the script in the form $1 $2 ... . $@ Contains a list of all the arguments passed to the script in the form $1$2... $? The return code of the last executed command. If the command was successful, $? is 0. $$ The process number (PID) of the current shell. Since no two processes can run simultaneously with the same PID it is a unique number in the short term. Used to generate unique lenames within a script. $! PID of the last process placed in the background. This number is needed to kill the process. $- Contains the shell options that are currently set. $ Absolute path name of the shell or script being executed. After execution, the shell or script path name is set to the last argument of the previous command.

Double Quotes, Forward Tics and Back tics


"..." 1) Allows variable and command substitution in strings with embedded spaces (double quote). 2) Protects all special characters except $,,`,\ 1) Does not allow variable and command substitution in strings (single quote). 2) Protects all special characters except itself. Command substitution (The backquote or backtick) (replaces a string with the output of the executed command) also achieved using $(command)

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

Conditional Logic on Files


-a -b -c -d -e -f -g -G -k -L -n -o -O -p -r -s -S -t -u -w -x -z le exists. le exists and is a block special le. le exists and is a character special le. le exists and is a directory. le exists (just the same as -a). le exists and is a regular le. le exists and has its setgid(2) bit set. le exists and has the same group ID as this process. le exists and has its sticky bit set. le exists and is a symbolic link. string length is not zero. Named option is set on. le exists and is owned by the user ID of this process. le exists and is a rst in, rst out (FIFO) special le or named pipe. le exists and is readable by the current process. le exists and has a size greater than zero. le exists and is a socket. le descriptor number ldes is open and associated with a terminal device. le exists and has its setuid(2) bit set. le exists and is writable by the current process. le exists and is executable by the current process. string length is zero.

Until
until test true do grep blob done

${param:?word} ${param#pattern} ${param##pattern}

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}

continue exit return

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

2 SIGINT 3 SIGQUIT 9 SIGKILL 11 SIGSEGV 15 SIGTERM 24 SIGTSTP

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.

Command: set -o options

Time based script execution


Cron Table

Compound Logical expressions


(expression) !expression exp1 -a exp2 exp1 &&exp2 exp1 -oexp2 exp1 ||exp2 True if expression is true. NOT expression. True if expression is false. Logical AND Logical AND Logical OR Logical OR

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

gmacs ignoreeof keyword markdirs monitor noclobber

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.

I/O Redirection and Pipes


silent running: myscript.ksh 2>&1 /dev/null

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.

> le < le >> le

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.

Upper or Lower Case Text for easy Testing


Upcasing
upcasevar=$(echo $variable | tr [a-z] [A-Z]) or typeset -u variable

Miscellaneous built-in commands


alias [-tx] [name[=value]] alias with no arguments prints the aliases. With arguments sets name to the same as value. echo [arg] Echoes arg to stdout. eval [arg] Evaluates the arguments as if the user had typed them in as text. eval command says to execute the output of command as if this had been typed into stdin. exec [arg] If arg is present, executes arg in place of this shell. (arg will replace this shell). exit [n] Terminates shell. The default is a return code of 0, otherwise n can be set from 0 through to 255 to give a specic return code.

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

Mail Notication Techniques


This is useful for notifying users when errors occur or a task has nished. mail -s "The Subject" $MAILOUT LIST < $MAIL FILE
A This card was created using L TEX. The card may be freely distributed under the terms of the GNU general public license. Revision: 0.872, Date: 19/01/2009 To contact me or download the latest version of this sheet please follow the links from: http://www.BenjaminEvans.net

You might also like