shell programming any command or a sequence of unix commands stored in a text file is called a shell...

8
Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command file or a script file. Usually the terms command or script is used when the file contains a sequence of commands. The term shell program usually identifies a file containing a more complicated arrangements of commands, often using the shell's conditional commands and other advanced features. Three ways to execute a command file: 1. Redirection 2. Supply the script name as an argument to the sh command 3.

Upload: agnes-peters

Post on 02-Jan-2016

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command

Shell Programming

• Any command or a sequence of UNIX commands stored in a text file is called a shell program.

• It is common to call this file a command file or a script file. Usually the terms command or script is used when the file contains a sequence of commands.

• The term shell program usually identifies a file containing a more complicated arrangements of commands, often using the shell's conditional commands and other advanced features.

• Three ways to execute a command file:1. Redirection2. Supply the script name as an argument to the sh command3.

Page 2: Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command

Executing a command file

• 1.        Since the shell reads commands interactively, use redirection to get the shell to read the commands from a file: ie

$sh < lsdir• This is a bit clumsy and also does not allow you to supply command line

arguments to the script which may be required in some cases.• 2.        The second method is to supply the script name as an argument to

the sh command. This reflects the special capablity that is built into the shell. Ie

• $ sh lsdir• One advantage is that you can supply arguments to the script ie

• $ sh lsdir /home/gb/exams• The drawback of the two methods shown is that the script has to be

explicitly run in order to execute the commands. • You cannot avoid using the shell to execute a script but you can have the

script executed automatically if you set the execute privileges to the script. • When you create a script in text editor, the execute permission is turned

off. This can be turned on to make the script text executable.

Page 3: Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command

Executing scripts

• 3.        Thus method 3 makes the script executable by changing the permission to the script file

• $ ls -l lsdir• -rw-r--r-- etc• $ chmod a+x lsdir• -rwxr-xr-x - etc•  • When you make the script executable, it must be located

somewhere in the search path. • Most search paths are constructed to look for programs in

the current directory. However, if this is not the case the script will not execute.

• You can also pass arguments to the executable script.

Page 4: Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command

Shells can store variables

• Shell can also use variables to store values: ie• $ kc=goran• $echo $kc• goran• (but no spaces are allowed) • If spaces are needed use quotes• $ kc="goran is here"• $echo $kc• goran is here • Curly brackets are needed when to surround the variable when it is

immediately followed by characters that are not part of the name.•  • $ kc=goran • $echo ${kc}Bez• goranBez

Page 5: Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command

Given an example Bourne Shell script – explain what is going on• TMP=/tmp• PIDLOG=${TMP}/pidlog$$• DATAFILE=${TMP}/date$$• DATEFILE=${TMP}/date$$• ERRLOG=${TMP}/ERRLOG• #shell function to clean up and exit after an

error• errexit()• { echo $1• date>> $ERRLOG• rm –f $PIDLOG $DATAFILE• exit• }• ok()• { while true• do• read ans• case $ans in• [yY]*) return 0 ;;• [nN]*) return 1;;• *) echo please answer y or

n ;;• esac• done• }• echo $$ > $PIDLOG• date > $DATAFILE• echo –n “Testing the errexit function”• echo normal termination• echo Please remove $PIDLOG and $DATAFILE

Page 6: Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command

Example Shell Script

• This program calls two functions errexit() and ok(). Comments: • -------------------------------------------------------• #files used in the program are given here• TMP=/tmp• PIDLOG=${TMP}/pidlog$$• DATAFILE=${TMP}/data$$• DATEFILE=${TMP}/date$$• ERRLOG=${TMP}/ERRLOG• -------------------------------------------------------• #shell function to clean up and exit after an error,• #this is the first function call.• errexit()• {• echo $1• date>> $ERRLOG• rm –f $PIDLOG $DATAFILE• exit• }

• Within the shell function the arguments are accessed using the notation $1, $2 etc.. Errexit is used to exit from the program when an error is encountered.

• In this event, errexit deletes temporary files, logs the error in a log file, and prints a message.

Page 7: Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command

The other function call

• # read a y/n response, this is the second function call• ok()• {• while true• do• read ans• case $ans in• [yY]*) return 0 ;;• [nN]*) return 1;;• *) echo please answer y or n ;;• esac• done• }

• Function ok() reads a line from the standard input, and returns TRUE or FALSE, depending on user input y/n.

Page 8: Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command

Continued

• echo $$ > $PIDLOG• date > $DATAFILE• echo –n “Testing the errexit function”• echo normal termination• echo Please remove $PIDLOG and $DATAFILE•  • The body of the shell program asks if you want to test the errexit function, in

which case user responds with y, conversely user responds with a n.• Function run:• The following are the possible results when the

program runs, Two options user Y or n• $ function_ssi• Test errexit function [y/n] y• Testing the errexit function•  • $ function_ssi• Test errexit function [y/n] n• Normal Termination• Please remove $PIDLOG and $DATAFILE