270 likes | 475 Views
UNIX – Shell Programming. The activities of the shell are not restricted to command interpretation alone. shell has a whole set of internal commands that can be strung together as a language most of the constructs are borrowed from C, but there are syntactical differences
E N D
UNIX – Shell Programming • The activities of the shell are not restricted to command interpretation alone. • shell has a whole set of internal commands that can be strung together as a language • most of the constructs are borrowed from C, but there are syntactical differences • shell programming is powerful because the external UNIX commands blend easily with the shell’s internal constructs in shell scripts © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • We will examine the programming features of the lowest common denominator of all shells • the Bourne shell • (/bin/sh) and its derivatives – the Korn shell (/bin/ksh) and Bash (/bin/bash). • The C shell uses totally different programming constructs © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • when a group of commands have to be executed regularly, they should be stored in a file, and the file executed as a shell script or a shell program. • it is not mandatory using the .sh extension for shell scripts; makes them easy to match with the wild cards. • shell script needs to have execute permission when invoked by its name. © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • it is not compiled to a separate executable file as a C program is • it runs in interpretive mode and in a separate child process • the calling process (often the login shell) forks a sub-shell which reads the script file and loads each statement into memory when it is to be executed • shell programs are slower than compiled programs • shell scripts are not recommended for number crunching, but to automate routine tasks • often scheduled to run non-interactively with cron (cron daemon) © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • System administrative tasks are often best handled by shell scripts • the reason why UNIX system administrator must be an accomplished shell programmer. © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • The She-Bang Line • the first line of the script (#!bin/bash) contains a string beginning with #! • this is not a comment line • it is called the interpreter line, hash-bang, or she-bang line • when the shell executes, the login shell reads this line first to determine the pathname of the program to be used for running the script © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • The She-Bang Line • the login shell spawns a Bourne sub-shell which actually executes each statement in sequence (in interpretive mode) • failure to provide the she-bang line, the login shell will spawn a child of its own type to run the script—which may not be the shell you want • you can also explicitly spawn a shell of your choice by running the program representing the shell with script name as argument: • sh script.sh © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • The She-Bang Line • when using the shell with the script name as argument, the Bourne sub-shell opens the file but ignores the interpreter line © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • read: Making Scripts Interactive • The read statement is the shell’s interactive tool for taking input from the user, making script interactive • read is used with one or more variables that are assigned by keyboard input. The statement • read name No $ here • will pause the script to take input from the standard input • a single read statement can be used with one or more variables • read pname flname © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • #!/bin/sh • echo "Enter the pattern to be searched: \c" • read pname • echo "Enter the file to be used: \c" • read flname • echo "Searching for $pname from file $flname" • grep "$pname" $flname • echo "Selected lines shown above" © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • {apache:~} emp1.sh • Enter the pattern to be searched: director • Enter the file to be used: shortlist • Searching for director from file shortlist • 9876: bill johnson :director :production:03/12/50:130000 • 2365:john woodcock :director :personnel :05/11/47:120000 • Selected lines shown above © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • Using Command Line Arguments • Scripts not using read can run non-interactively and be used with redirection and pipelines • such scripts take their input from command line arguments • assigned to certain special “variables”—positional parameters • the first argument is available in $1, the second $2, and so on • in addition to the positional parameters, there are a few other special parameters used by the shell © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • Using Command Line Arguments • $* -- Stores the complete set of positional parameters as a single string • $# -- It is set to the number of arguments specified; lets you design scripts that check whether the right number of arguments have been entered • $0 – Holds the script filename itself. © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • Using Command Line Arguments • #!/bin/sh • echo "Program: $0" • echo "The number of arguments specified is $#" • echo "The argumetns are $*" • grep $1 $2 • echo "\nJob Over" © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • Using Command Line Arguments • {apache:~} emp2.sh director shortlist • Program: ./emp2.sh • The number of arguments specified is 2 • The argumetns are director shortlist • 9876: bill johnson :director :production:03/12/50:130000 • 2365:john woodcock :director :personnel :05/11/47:120000 © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • Using Command Line Arguments • $1, $2… Positional parameters representing arguments • $# Number of arguments • $0 Name of executed command • $* Complete set of positional parameters in a string • “$@” Each quoted string is treated as a separate arg • $? Exit status of the last command • $$ PID of current shell • $! PID of last background job © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • exit and $? : Exit status of a Command • All programs and shell scripts return a value called the exit status to the caller, often the shell • The shell waits for a command to complete execution and then picks up this value from the process table • exit 0 – True, everything went fine • exit 1 – False; something went wrong © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • exit and $? : Exit status of a Command • A program is designed to return a true exit status when it runs successfully and false otherwise • what constitutes success or failure is determined by the designer of the porgram • for example, when grep could not locate a pattern; we said that the command failed; meaning the designer of grep made the program returns false exit status on failing to locate a pattern © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • exit and $? : Exit status of a Command • A program is designed to return a true exit status when it runs successfully and false otherwise • what constitutes success or failure is determined by the designer of the porgram • for example, when grep could not locate a pattern; we said that the command failed; meaning the designer of grep made the program returns false exit status on failing to locate a pattern © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • exit and $? : Exit status of a Command • A program is designed to return a true exit status when it runs successfully and false otherwise • the pattern $? stores the exit status of the last command; • the value of zero (0) indicates success and non-zero when the command fails • if no exit is specified, then $? is set to to zero (true) © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • exit and $? : Exit status of a Command • /dev/null the null device that discards all data written to it • {apache:~} grep director emp.lst > /dev/null; echo $? • grep: can't open emp.lst • 2 • {apache:~} grep director shortlist > /dev/null; echo $? • 0 • {apache:~} grep manager shortlist > /dev/null; echo $? • 1 © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • exit and $? : Exit status of a Command • The if and while constructs implicitly check $? to control the flow of execution. • it is good programming practice to place exit statements with meaningful exit values at appropriate points in script • for example, if an important file doesn’t exist or can’t be read, there is not point in continuing with script execution • use exit 1 at that point so the next program knows that the previous program failed © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • The Logical Operators && and || -- Conditional Execution • cmd1 && cmd2 cmd2 executed if cmd1 succeeds • cmd1 || cmd2 cmd2 executed if cmd1 fails • When && is used to delimit two commands, cmd2 is executed only when cmd1 succeeds. • {apache:~} grep director shortlist > /dev/null && echo "pattern found in file" • pattern found in file © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • The Logical Operators && and || -- Conditional Execution • cmd1 && cmd2 cmd2 executed if cmd1 succeeds • cmd1 || cmd2 cmd2 executed if cmd1 fails • The || operators does the opposite; the second command is executed only when the first fails: • {apache:~} grep manager shortlist > /dev/null || echo "pattern found in file" • pattern found in file © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
UNIX – Shell Programming • Shell Scripts • The Logical Operators && and || -- Conditional Execution • To display a message before invoking exit, you need to group commands, but remember to use only curly braces • this will execute the enclosed commands in the current shell: • grep joker /etc/passwd || { echo "Pattern not found" ; exit 0; } • Use of paranthesis would not terminte the script. If the {} is executed at the shell prompt, you would be logged out. • The logical operators are recommended for making simple decisions; otherwise use the if statement. © 2006 The McGraw-Hill Companies, Inc. All rights reserved.