Note: Like the /bin/csh shell page, here is a good introduction to /bin/sh programming. Credit: I found this here at North Carolina University... IntroductionA shell is a command line interpreter. It takes commands and executes them. As such, it implements a programming language. The Bourne shell can be used to create shell scripts, i.e., programs that are interpreted/executed by the shell. You can write shell scripts with the C shell; however, this is not covered here. Creating a ScriptSuppose you often type the command find . -name file -print and you'd rather type a simple command, say sfind file Create a shell script like this: % cd ~/bin ObservationsThis quick example is far from adequate but some observations:
Fundamentals#!/bin/shAll Bourne Shell scripts should begin with the sequence #!/bin/sh From the man page for exec(2): On the first line of an interpreter script, following the "#!", is the name of a program which should be used to interpret the contents of the file. For instance, if the first line contains "#!/bin/sh", then the con- tents of the file are executed as a shell script. You can get away without this, but you shouldn't. All good scripts state the interpretor explicitly. Long ago there was just one (the Bourne Shell) but these days there are many interpreters -- csh, ksh, bash, and others. CommentsComments are any text beginning with the pound (#) sign. A comment can start anywhere on a line and continue until the end of the line. Search PathAll shell scripts should include a search path specification: PATH=/usr/ucb:/usr/bin:/bin; export PATH A PATH specification is recommended -- often times a script will fail for some people because they have a different or incomplete search path. The Bourne Shell does not export environment variables to children unless explicitly instructed to do so by using the export command. Argument CheckingA good shell script should verify that the arguments supplied (if any) are correct. if [ $# -ne 3 ]; then This script requires three arguments and gripes accordingly. Exit statusAll Unix utilities should return an exit status. # is the year out of range for me? A non-zero exit status indicates an error condition of some sort while a zero exit status indicates things worked as expected. On BSD systems there's been an attempt to categorize some of the more common exit status codes. See /usr/include/sysexits.h. Using exit statusExit codes are important for those who use your code. Many constructs test on the exit status of a command. The conditional construct is: if command; then For example, if tty -s; then Your code should be written with the expectation that others will use it. Making sure you return a meaningful exit status will help. Stdin, Stdout, StderrStandard input, output, and error are file descriptors 0, 1, and 2. Each has a particular role and should be used accordingly: # is the year out of range for me? Error messages should appear on stderr not on stdout! Output should appear on stdout. As for input/output dialogue: # give the fellow a chance to quit Note: this code behaves differently if there's a user to communicate with (ie. if the standard input is a tty rather than a pipe, or file, or etc. See tty(1)). <!-- -------------------------------------------------------------- -->Language Constructsfor - loop iterationSubstitute values for variable and perform task: for variable in word ... For example: for i in `cat $LOGS` Alternatively you may see: for variable in word ...; do command; done case - multiway branchSwitch to statements depending on pattern match: case word in For example: case "$year" in if - conditional executionTest exit status of command and branch: if command For example: if [ $# -ne 3 ]; then Alternatively you may see: if command; then command; [ else command; ] fi while/until - tested iterationsRepeat task while command returns good exit status. {while | until} command For example: # for each argument mentioned, purge that directory Alternatively you may see: while command; do command; done VariablesVariables are sequences of letters, digits, or underscores beginning with a letter or underscore. To get the contents of a variable you must prepend the name with a $. Numeric variables (eg. like $1, etc.) are positional vari- ables for argument communication. Variable AssignmentAssign a value to a variable by variable=value. For example: PATH=/usr/ucb:/usr/bin:/bin; export PATH or TODAY=`(set \`date\`; echo $1)` Exporting VariablesVariables are not exported to children unless explicitly marked. # We MUST have a DISPLAY environment variable Likewise, for variables like the PRINTER which you want hon- ored by lpr(1). From a user's .profile: PRINTER=PostScript; export PRINTER Note that the Cshell automatically exports all environment variables. Referencing VariablesUse $variable (or, if necessary, ${variable}) to reference the value. # Most user's have a /bin of their own The braces are required for concatenation constructs. $p_01 is the value of the variable "p_01". ${p}_01 is the value of the variable "p" with "_01" pasted onto the end. Conditional Reference${variable-word} If the variable has been set, use it's value, else use word. POSTSCRIPT=${POSTSCRIPT-PostScript}; If the variable has been set and is not null, use it's value, else use word. These are useful constructions for honoring the user environment, i.e., the user of the script can override variable assignments. Compare to programs like lpr(1), which honors the PRINTER environment variable. You can do the same trick with your shell scripts. ${variable:?word} If variable is set use it's value, else print out word and exit. Useful for bailing out. ArgumentsCommand line arguments to shell scripts are positional variables: $0, $1, ... The command and arguments, with $0 the command and the rest the arguments. $# The number of arguments. $*, [email protected] All the arguments as a blank separated string. Watch out for "$*" vs. "[email protected]". Commands for Variablesshift Shift the postional variables down one and decrement number of arguments. set arg arg ... Set the positional variables to the argument list. Command line parsing uses shift: # parse argument list A use of the set command: # figure out what day it is Special Variables$$ Current process id. This is very useful for constructing temporary files. tmp=/tmp/cal0$$ The exit status of the last command. $command Quotes/Special CharactersSpecial characters to terminate words: ; & ( ) | ^ < > new-line space tab These are for command sequences, background jobs, etc. To quote any of these use a backslash (\) or bracket with quote marks ("" or ''). Single QuotesWithin single quotes all characters are quoted, including the backslash. The result is one word. grep :${gid}: /etc/group | awk -F: '{print $1}' Double QuotesWithin double quotes you have variable substitution (i.e., the dollar sign is interpreted) but no file name generation (i.e., * and ? are quoted). The result is one word. if [ ! "${parent}" ]; then Back QuotesBack quotes mean run the command and substitute the output. if [ "`echo -n`" = "-n" ]; then and TODAY=`(set \`date\`; echo $1)` FunctionsFunctions are a powerful feature that aren't used often enough. Syntax is name () For example: # Purge a directory Within a function the positional parmeters $0, $1, etc. are the arguments to the function (not the arguments to the script). Within a function use return instead of exit. Functions are good for encapsulations. You can pipe, redi- rect input, etc. to functions. For example: # deal with a file, add people one at a time Sourcing CommandsYou can execute shell scripts from within shell scripts. A couple of choices: sh command This runs the shell script as a separate shell. For example, on Sun machines in /etc/rc: sh /etc/rc.local . command This runs the shell script from within the current shell script. For example: # Read in configuration information What are the virtues of each? What's the difference? The second form is useful for configuration files where environment variables are set for the script. For example: for HOST in $HOSTS; do Using configuration files in this manner makes it possible to write scripts that are automatically tailored for differ- ent situations. Some TricksTestThe most powerful command is test(1). if test expression; then and (note the matching bracket argument) if [ expression ]; then On System V machines this is a builtin (check out the com- mand /bin/test). On BSD systems (like the Suns) compare the command /usr/bin/test with /usr/bin/[. Useful expressions are: test { -w, -r, -x, -s, ... } filename is file writeable, readable, executeable, empty, etc? test n1 { -eq, -ne, -gt, ... } n2 are numbers equal, not equal, greater than, etc.? test s1 { =, != } s2 Are strings the same or different? test cond1 { -o, -a } cond2 Binary or; binary and; use ! for unary negation. For example if [ $year -lt 1901 -o $year -gt 2099 ]; then Learn this command inside out! It does a lot for you. String matchingThe test command provides limited string matching tests. A more powerful trick is to match strings with the case switch. # parse argument list Of course getopt would work much better. SysV vs BSD echoOn BSD systems to get a prompt you'd say: echo -n Ok to proceed?; read ans On SysV systems you'd say: echo Ok to procede? \c; read ans In an effort to produce portable code we've been using: # figure out what kind of echo to use Is there a person?The Unix tradition is that programs should execute as quietly as possible, especially for pipelines, cron jobs, etc. User prompts aren't required if there's no user. # If there's a person out there, prod him a bit. The tradition also extends to output. # If the output is to a terminal, be verbose Beware: just because stdin is a tty that doesn't mean that stdout is too. User prompts should be directed to the user terminal. # If there's a person out there, prod him a bit. Have you ever had a program stop waiting for keyboard input when the output is directed elsewhere? Creating InputWe're familiar with redirecting input. For example: # take standard input (or a specified file) and do it. alternatively, redirection from a file: # take standard input (or a specified file) and do it. You can also construct files on the fly. rmail bsmtp << EOF Note that variables are expanded in the input. String ManipulationsOne of the more common things you'll need to do is parse strings. Some tricks: TIME=`date | cut -c12-19` With some care, redefining the input field separators can help. #!/bin/sh DebuggingThe shell has a number of flags that make debugging easier: sh -n command Read the shell script but don't execute the commands, i.e., check syntax. sh -x command Display commands and arguments as they're executed. In a lot of my shell scripts you'll see # Uncomment the next line for testing Based on An Introduction to Shell Programing by: Reg Quinton <[email protected]> Computing and Communications Services The University of Western Ontario London, Ontario N6A 5B7 CanadaThe original version of this document is here. |