CS248 Menu Buttons

UNIX Programming

"Chapter Two - Shell Programming"

Warning! Read This First!

When executing files from the directory they are stored in, type ./filename

The file downloadable with the book called my_name, has an error on line 22. It should be else, not elif.

The cd_db file, in the list_tracks() procedure, inside the inside else clause, has { }. Remove the { and } and the program should execute.

Chapter Outline

Lecture Notes

Shell Programming

The shell has similarities to the DOS command processor Command.com (actually Dos was design as a poor copy of UNIX shell), it's actually much more powerful, really a programming language in its own right.

A shell is always available on even the most basic UNIX installation. You have to go through the shell to get other programs to run. You can write programs using the shell. You use the shell to administrate your UNIX system. For example:

is a short shell program to get a long listing of the present directory and route the output through the more command.

What is a Shell?

A shell is a program that acts as the interface between you and the UNIX system, allowing you to enter commands for the operating system to execute.

Here are some common shells.

Pipes and Redirection

Pipes connect processes together. The input and output of UNIX programs can be redirected.

Redirecting Output

The > operator is used to redirect output of a program. For example:

redirects the output of the list command from the screen to the file lsoutput.txt.

To append to a file, use the >> operator.

Redirecting Input

You redirect input by using the < operator. For example:

Pipes

We can connect processes together using the pipe operator ( | ). For example, the following program means run the ps program, sort its output, and save it in the file pssort.out

The sort command will sort the list of words in a textfile into alphbetical order according to the ASCII code set character order.

The Shell as a Programming Language

You can type in a sequence of commands and allow the shell to execute them interactively, or youu can sotre these commands in a file which you can invoke as a program.

Interactive Programs

A quick way of trying out small code fragments is to just type in the shell script on the command line. Here is a shell program to compile only files that contain the string POSIX.

Creating a Script

To create a shell script first use a text editor to create a file containing the commands. For example, type the following commands and save them as first.sh

Note: commands start with a #.

The line

is special and tells the system to use the /bin/sh program to execute this program.

The command

Causes the script program to exit and return a value of 0, which means there were not errors.

Making a Script Executable

There are two ways to execute the script. 1) invoke the shell with the name of the script file as a parameter, thus:

Or 2) change the mode of the script to executable and then after execute it by just typing its name. Actually, you may need to type: to make the file execute unles the path variable has your directory in it.

Shell Syntax

The modern UNIX shell can be used to write quite large, structured programs.

Variables

Variables are generally created when you first use them. By default, all variables are considered and stored as strings. Variable names are case sensitive.

Quoting

Normally, parameters are separated by white space, such as a space. Single quot marks can be used to enclose values containing space(s). Type the following into a file called quot.sh

make sure to make it executable by typing the command:

The results of executing the file is:

How It Works

The variable myvar is created and assigned the string Hi there. The content of the variable is displyed using the echo $. Double quotes don't effect echoing the value. Single quotes and backslash do.

Environment Variables

When a shell starts, some variables are initialized from values in the environment. Here is a sample of some of them.


Parameter Variables

If your script is invoked with parameters, some additional variables are created.

The following shows the difference between using the variable $* and $@

notice that the first line of the above has a space between the firsr ' and the second '.

Now try your hand at typing a shell script

Carefully type the following into a file called: try_variables

make sure to make it executable by typing the command:

Execute the file with parameters by typing: The results of executing the file is:

How It Works

It creates the variable salutation, displays its value, and some parameter variables.

Conditions

All programming languages have the ability to test conditions and perform different actions based on those conditions. A shell script can test the exit code of any command.

The test, or []Command

Here is how to check for the existance of the file fred.c using the test and using the [] command.

You can even place the then on the same line as the if, if youu add a semicolon before the word then.

Here are the conditon types that can be used with the test command. There are string comparison.

There are arithmetic comparison.

There are file conditions.

Control Structures

The shell has a set of control structures.

if

The if statement is vary similar other programming languages except it ends with a fi.

elif

the elif is better known as "else if". It replaces the else part of an if statement with another if statement. You can try it out by using the following script.

How It Works

The above does a second test on the variable timeofday if it isn't equal to yes.

A Problem with Variables

If a variable is set to null, the statement

looks like which is illegal. This problem can be fixed by using double quotes around the variable name..

for

The for construct is used for looping through a range of values, which can be any set of strings. The syntax is:

Try out the following script: When executed, the output should be:

How It Works

The above example creates the variable foo and assigns it a different value each time around the for loop.

How It Works

Here is another script which uses the $(command) syntax to expand a list to chap3.txt, chap4.txt, and chap5.txt and print the files.

while

While loops will loop as long as some condition exist. OF course something in the body statements of the loop should eventually change the condition and cause the loop to exit. Here is the while loop syntax.

Here is a whil loop that loops 20 times.

How It Works

The above script uses the [ ] command to test foo for <= the value 20. The line

increments the value of foo each time the loop executes..

until

The until statement loops until a condition becomes true! Its syntax is:

Here is a script using until.

case

The case statement allows the testing of a variable for more then one value. The case statement ends with the word esac. Its syntax is:

Here is a sample script using a case statement:

How It Works

The value in the varaible timeofday is compared to various strings. When a match is made, the associated echo command is executed.

Here is a case where multiple strings are tested at a time, to do the some action.

How It Works

The above has sever strings tested for each possible statement.

Here is a case statement that executes multiple statements for each case.

How It Works

When a match is found to the variable value of timeofday, all the statements up to the ;; are executed.

Lists

To test for multiple conditions, we can use nested if or if/elif.

The AND List

Alolows us to execute a series of command. Each command is only execute if the previous commands have succeeded. An AND list joins conditions by using &&.

Her is a sample AND list:

How It Works

The touch command creates an empty file. the rm come remove a file. So, before we start, file_one exists and file_two doesn't. The AND list finds the file_one, and echos the word hello, but it doesn't find the file file_two. Therefore the overall if fails and the else clause is executed.

The OR List

The OR list construct allows us to execute a series of commands until one succeeds!

Here is a sample Or list

How It Works

The above script removes the file file_one, then test for and fails to find the file_one, but does successfully echo hello. It then executes the then statement echoing in if.

Statement Blocks

Multiple statements can be placed inside of { } to make a statement block.

Functions

You can define functions inthe shell. The syntax is:

Here is a sample function and its execution.

How It Works

When the above script runs, it defines the funcion foo, then script echos script starting, then it runs the functions foo which echos Function foo is executing, then it echo script ended.

Here is another sample script with a function in it. Save it as my_name

How It Works

When my_name is execute with the statement:

. gives the output of:

Commands

You can execute normal command and built-in commands from a shell script. Built-in commands are defined and only run inside of the script.

break

It is used to escape from an enclosing for, while or until loop before the controlling condition has been met.

The : Command

The colon command is a null command. It can be used for an alias for true..

continue

The continue command makes the enclosing for, while, or until loop continue at the next iteration.

The . Command

The dot command executes the command in the current shell:

.

echo

The echo command simply outputs a string to the standard output device followed by a newline character.

eval

The eval command evaluates arguments and give s the results.

exec

The exec command can replace the current shell with a different program. It can also modify the current file descriptors.

exit n

The exit command causes the script to exit with exit code n. An exit code of 0 means success. Here are some other codes.

export

The export command makes the variable named as its parameter available in subshells.

expr

The expr command evaluates its arguments as an expression.

Here are some of its expression evaluations

printf

The printf command is only available in more recent shells. It works similar to the echo command. Its general form is:

Here are some characters and format specifiers.


return

The return command causes functions to return. It can have a value parameter which it returns.

set

The set command sets the parameter variables for the shell.

shift

The shift command moves all the parameters variables down by one, so $2 becomes $1, $3 becomes $2, and so on.

trap

The trap command is used for secifying the actions to take on receipt of signals. It syntax is:

Here are some of the signals.

How It Works

The try it out section has you type in a shell script to test the trap command. It creates a file and keeps saying that it exists until youu cause a control-C interrupt. It does it all again.

unset

The unset command removes variables or functions from the environment.

Command Execution

The result of $(command) is simply the output string from the command, which is then available to the script.

Arithmetic Expansion

The $((...)) is a better alternative to the expr command, which allows simple arithmetic commands to be processed.

Parameter Expansion

Using { } around a variable to protect it against expansion.

Here are some of the parameter expansion

How It Works

The try it out exercise uses parameter expansion to demonstrate how parameter expansion works.

Here Documents

A here document is a special way of passing input to a command from a shell script. The document starts and ends with the same leader after <<. For example:

How It Works

It executes the here document as if it were input commands.

Debugging Scripts

When an error occurs in a script, the shell prints out the line number with an error. You can use the set command to set various shell option. Here are some of them.

Putting It All Together

The rest of this chapter is about designing a CD database application.

Requirements

The system should store basic information about each CD, search for CDs, and update or add new CDs.

Design

The three requirements--updating, searching and displaying the CD data--suggest that a simple menu willbe adequate. Here is the example titles file.

Here is the associated track file.

Notes

The code for the CD database is included in the try it out section. The trap command allows the user to use Ctrl-C.

Summary

By the time you enter the CD database application, you will know that programs can be written using just the shell language. The shell is used for much of Linux system administration.


CS 248 - UNIX Programming Web Site Menu
Information | Syllabus | Schedule | Online "Lectures" | Projects | Quizzes | Web Board



Copyright © 2001 by James L. Fuller, all rights reserved.