unix bourne shell scripting

Download UNIX Bourne Shell Scripting

Post on 29-Nov-2014




4 download

Embed Size (px)


UNIX Bourne Shell Scripting

UNIX Bourne Shell ScriptingKen Steube Earth Systems Science Computational Centre The University of Queensland Brisbane, Australia uuencode test.txt test.txt | mailx -s "without x" nishantp@techmahindra.com These notes teach you how to write and run Bourne shell scripts on any UNIX computer. What do you need to know to follow along? This was originally written as a second class in UNIX. The first class taught how to use the basic UNIX commands (like sed, grep and find) and this class teaches how to combine these tools to accomplish bigger tasks. In addition to the material in this course you might be interested in the Korn shell (ksh) and the Bourne again shell (bash), both of which are excellent shells that enchance the original Bourne shell. These alternate shells are upwardly-compatible with the Bourne shell, meaning that a script written for sh can run in ksh or bash. However, there are additional features in bash and ksh that are not available in the Bourne shell. The focus of this guide is to get you to understand and run some Bourne shell scripts. On several pages there are example scripts for you to run. On most of these pages there is a link you can click on (with the right mouse button) and download the script to your computer and run it. You will learn several things:

Ability to automate tasks, such as o Software install procedures o Backups o Administration tasks o Periodic operations on a database via cron o Any repetetive operations on files Increase your general knowledge of UNIX o Use of environment o Use of UNIX utilities o Use of features such as pipes and I/O redirection

For example, I recently wrote a script to make a backup of one of the subdirectories where I was developing a project. I quickly wrote a shell script that uses /bin/tar to create an archive of the entire subdirectory and then copy it to one of our backup systems at my computer center and store it under a subdirectory named according to today's date.

_______________________________________________________________________ _ 1

UNIX Bourne Shell Scripting

As another example, I have some software that runs on UNIX that I distribute and people were having trouble unpacking the software and getting it running. I designed and wrote a shell script that automated the process of unpacking the software and configuring it. Now people can get and install the software without having to contact me for help, which is good for them and good for me, too! For shell script experts one of the things to consider is whether to use the Bourne shell (or ksh or bash), the C shell, or a richer scripting language like perl or python. I like all these tools and am not especially biased toward any one of them. The best thing is to use the right tool for each job. If all you need to do is run some UNIX commands over and over again, use a Bourne or C shell script. If you need a script that does a lot of arithmetic or string manipulation, then you will be better off with perl or python. If you have a Bourne shell script that runs too slowly then you might want to rewrite it in perl or python because they can be much faster. Historically, people have been biased toward the Bourne shell over the C shell because in the early days the C shell was buggy. These problems are fixed in many C shell implementations these days, especially the excellent 'T' C shell (tcsh), but many still prefer the Bourne shell. There are other good shells available. I don't mean to neglect them but rather to talk about the tools I am familiar with. If you are interested also in learning about programming in the C shell I also have a comparison between features of the C shell and Bourne shell.

Table of Contents:1. Review of a few Basic UNIX Topics (Page 1) 2. Storing Frequently Used Commands in Files: Shell Scripts (Page 6) 3. More on Using UNIX Utilities (Page 9) 4. Performing Search and Replace in Several Files (Page 11) 5. Using Command-line Arguments for Flexibility (Page 14) 6. Using Functions (Page 30) 7. Miscellaneous (Page 38) 8. Trapping Signals (Page 43) 9. Understanding Command Translation (Page 50) 10. Writing Advanced Loops (Page 59) 11. Creating Remote Shells (Page 67) 12. More Miscellaneous (Page 73) 13. Using Quotes (Page 75)

_______________________________________________________________________ _ 2

UNIX Bourne Shell Scripting

Section 1: Review of a few Basic UNIX TopicsShell scripting involves chaining several UNIX commands together to accomplish a task. For example, you might run the 'date' command and then use today's date as part of a file name. I'll show you how to do this below. Some of the tools of the trade are variables, backquotes and pipes. First we'll study these topics and also quickly review a few other UNIX topics.


Topics covered: storing strings in variables Utilities covered: echo, expr To try the commands below start up a Bourne shell:/bin/sh

A variable stores a string (try running these commands in a Bourne shell)name="John Doe" echo $name

The quotes are required in the example above because the string contains a special character (the space) A variable may store a numbernum=137

The shell stores this as a string even though it appears to be a number A few UNIX utilities will convert this string into a number to perform arithmeticexpr $num + 3

Try defining num as '7m8' and try the expr command again What happens when num is not a valid number? Now you may exit the Bourne shell withexit

_______________________________________________________________________ _ 3

UNIX Bourne Shell Scripting

I/O Redirection

Topics covered: specifying the input or capturing the output of a command in a file Utilities covered: wc, sort The wc command counts the number of lines, words, and characters in a filewc /etc/passwd wc -l /etc/passwd

You can save the output of wc (or any other command) with output redirectionwc /etc/passwd > wc.file

You can specify the input with input redirectionwc < /etc/passwd

Many UNIX commands allow you to specify the input file by name or by input redirectionsort /etc/passwd sort < /etc/passwd

You can also append lines to the end of an existing file with output redirectionwc -l /etc/passwd >> wc.file


Topics covered: capturing output of a command in a variable Utilities covered: date The backquote character looks like the single quote or apostrophe, but slants the other way It is used to capture the output of a UNIX utility A command in backquotes is executed and then replaced by the output of the command Execute these commandsdate save_date=`date` echo The date is $save_date

Notice how echo prints the output of 'date', and gives the time when you defined the save_date variable

_______________________________________________________________________ _ 4

UNIX Bourne Shell Scripting

Store the following in a file named backquotes.sh and execute it (right click and save in a file)#!/bin/sh # Illustrates using backquotes # Output of 'date' stored in a variable Today="`date`" echo Today is $Today

Execute the script withsh backquotes.sh

The example above shows you how you can write commands into a file and execute the file with a Bourne shell Backquotes are very useful, but be aware that they slow down a script if you use them hundreds of times You can save the output of any command with backquotes, but be aware that the results will be reformated into one line. Try this:LS=`ls -l` echo $LS


Topics covered: using UNIX pipes Utilities covered: sort, cat, head Pipes are used for post-processing data One UNIX command prints results to the standard output (usually the screen), and another command reads that data and processes itsort /etc/passwd | head -5

Notice that this pipe can be simplifiedcat /etc/passwd | head -5

You could accomplish the same thing more efficiently with either of the two commands:head -5 /etc/passwd head -5 < /etc/passwd

For example, this command displays all the files in the current directory sorted by file sizels -al | sort -n -r +4

The command ls -al writes the file size in the fifth column, which is why we skip the first four columns using +4. The options -n and -r request a numeric sort (which is different than the normal alphabetic sort) in reverse order

_______________________________________________________________________ _ 5

UNIX Bourne Shell Scripting


Topics covered: processing columnar data Utilities covered: awk The awk utility is used for processing columns of data A simple example shows how to extract column 5 (the file size) from the output of ls -lls -l | awk '{print $5}'

Cut and paste this line into a Bourne shell and you should see a column of file sizes, one per file in your current directory. A more complicated example shows how to sum the file sizes and print the result at the end of the awk runls -al | awk '{sum = sum + $5} END {print sum}'

In this example you should see printed just one number, which is the sum of the file sizes in the current directory.

_______________________________________________________________________ _ 6

UNIX Bourne Shell Scripting

Section 2: Storing Frequently Used Commands in Files: Shell Scripts

Shell Scripts

Topics covered: storing commands in a file and executing the file Utilities covered: date, cal, last (shows who has logged in recently) Store the following in a file named simple.sh and execute it#!/bin/sh # Show some useful info at the start of the day date echo Good morning $USER cal last | head -6

Shows current date, calendar, an


View more >