1 / 8

Shell Programming

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.

Download Presentation

Shell Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 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.

  2. 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.

  3. 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.

  4. 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

  5. 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

  6. 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.

  7. 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.

  8. 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

More Related