Shell Scripts

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Shell Scripts as PDF for free.

More details

  • Words: 4,943
  • Pages: 20
Shell Scripts As noted earlier, Unix shells are all fully-featured programming languages as well as simple ways of invoking programs. A file containing a number of shell commands is called a shell script. Simple shell scripts are simply a sequence of simple commands (such as are many .profile files). However, a full set of iterative and conditional constructs are also available. The very big advantage of shell scripts is that they provide a simple way of automating a number of otherwise manual tasks. If you need to type a sequence of commands in more than twice, you probably want to write shell script. Remember, we programmers are all tool makers; we like making things easier for ourselves. The shell incorporates a powerful programming feature. It facilitates all programming constructs such as looping, conditional statements, variable declaration, function etc. A shell program is a series of UNIX commands that we have learned. The difference being that we had till now been using these commands to perform an isolated task. But in a shell program, we will combine these commands to perform complex task. A sequence of shell commands stored in a file, the name of which is later used to execute the stored sequence, is known as a shell script. We do not need a special compiler to execute the shell scripts. These are interpreted and executed by the shell itself. In shell script it is not necessary to give the name with extension .sh. But customarily name them .sh extension. Shell Variables Shell variable are similar to the variable in other programming languages. They are used to store and manipulate various types of data within a shell program. The shell variable can hold a values of any data type, and its data type does not have to be set when declaring the variable. Its data type is ascertained by the type of data we store in the variable. If we assign a character value the variable become a character variable. a=’A’ Values bounded by single quotes Note that there should be no space before and after the assignment operator (=). a=10 Numaric Variable a= “Amit lal” Float Variable The rules for naming variable are similar as “C programming languages”. The values is stored in a variable is retrieved by preceding the variable name with a doller ($) sign. Example: a=10.25 echo “The values of a is $a” 10.25 b=$a echo “The value of b is $b” 10.25 The result of ls-l command is assigned to the variable a.

1

a= ‘ls-l’ echo $a Note : Remember that a command can be executed within another command by enclosing the letter in single quotes( ‘ ’). # This program display the date and time, current directory and its content. echo “Todays Date is” date echo “Your Current Working Directory is” pwd echo “It contains” ls| more Execution The script can be executed by using the command .sh followed by the filename. For example the pcc can be invoked by the following command: $ pcc.sh You can used the chmode command to execute the file. $ chmode +x pcc.sh Once this is done you can execute the shell script giving the filename in the command line. $ pcc.sh When we execute a shell script, the login shell creates a new shell and the command in the shell script is executed in this new shell. After executing your shell script, the new shell terminates and the login shell again takes over the control. The && And || Operators The operators && and || can be used to control execution of a command depending on the success or failure of the previous command. The && command is used when we want to execute the second command only if the first command succeeds. For example, the following statement will print the message “book found” if the grep command succeeds. grep ‘UNIX’ book.lst && echo “book found” The || command is just opposite of &&. It is used when we want to execute the second command only if the first command fails. For example, the following statement will print the message “book not found” if the grep command fails. grep ‘UNIX’ book.lst || echo “book found” Computation using expr The command expr is used in both arithmatic computation and string handling. The expr command computes an expression and returns its result for example $ expr 5+3  Invalid expessions Valid expressions

2

$ expr 5 + 3 8 $ expr 5 - 3 2 $ expr 5 \* 3 15 $ expr 5 / 3 1 $ expr 5 % 3 2 A space on either side of the operator is essential in an expr statement. The * is escaped using a \ so that the shell does not interprets it as one of its meta characters. In string handling, expr can be used to compute the length of the string, locate the position of a character in a string or extract a substring. Here expr requires two expressions separated by a colon (:). The string on which a function to be performed is placed on the left side of the colon(:) and a regular expression is placed on the left side. The expression “.*” is used to compute the length of a string for example $ expr “Pandharipande Computes”: ‘.*’ 23 Note that there should be a space on either side of the colon(:) test The test statement tests a given condition. It returns 1 if the condition is falseand return 0 if the condition is true. It uses certain operators known as test operators, for comparison. For example –eq is used for “equal to”, -gt for “greater than” etc. Example: $ a=10 ; b=20 $ text $b –gt $a $ echo $? 0  20 is greater than 10 $ text $a –eq $b $ echo $? 1  20 is not equal to 10 Note the use of test in the above sequence. There should be space on either side of the test operators. We used $? To find out the result of test command. The test word can be replaced by the use of [ ], enclosing the condition within brackets as follows: $ a=10 ; b=20

3

$ [$b –gt $a] $ echo $? 0 “test operator” Operator -eq -ne -gt -lt -ge -le -n str str -2 str str1=str2 str1!=str2

Implies equal to not equal to greater than less than grater than equal to less than equal to string is not a null string same as above string str is a null string string str1 is equal to str2 string str1 is not equal to str2

Commenting in Shell Script The # at the start of he line indicates a comment that goes to the end of line. Comments are used to tell whoever reads your shell script what the task is going on. Whatever is written using comment ignores the shell script. This is the Comment symbol in Shell #. Shell Programs # Write a shell script to print the area of circle Clear echo “Enter the Radius of a Circle : - ” read radius echo ‘expr 22 \* $radius \* $rad / 7’ echo “Area of Circle: echo “ ” Output Enter the Radius of Circle: 3 Area of Circle: 28 # Write a shell script print rhombus, rectangle and triangle. Clear echo echo “Enter height and base of a triangle :-” read height read base echo “Area of triangle :-”

4

echo ‘expr $base \* $height / 2’ echo “ ” echo “Enter length and breadth of a rectangle” read length read breadth echo “Area of Rectangle : “ echo ‘expr $length \* $breadth’ echo “Enter the diameter: ” read d1 echo “Enter the diameter: ” read d2 echo “Area of Rhombus is :” echo ‘expr 1 \* $d1 \* $d2 / 2’ Output Enter the height and base of a triangle:4 5 Area of Triangle:- 10 Area of Rectangle : Enter length and breadth of a rectangle 3 3 Area of Rectangle : 9 Area of Rhombus is: Enter the diameter: 6 Enter the diameter : 7 Area of Rhombus is : 21 The if Conditional The if statement in used to check the condition. It is used to takes two-way decisions depending on the fulfillment of a certain condition. In the shell, the statement uses the following form, much like the one used in other languages: if then <executes commands> else <executes commands> fi

5

if evaluates a condition which accomplish its command line. If the condition is fulfilled (i.e. returns a true exit status), then the sequence of commands following it is executes. Then Keyword then and fi which must necessarily accompany every if conditional. The construct also provides for an alternate action using the optional keyword else. This is not always required, and the simplex form of the if statement: if then <execute commands> fi # Enter the following sequence at the prompt to check whether the pattern director can be locate in the file.lst. $ if grep “director” emp.lst > then echo “Pattern Found – Job Over” >else echo “Pattern not found” >fi 9876 | jai sharma | director | production | 12/03/85 | 7000 2365 | ram pande | director | personnel |11/05/85 | 7800 1006 | lalit choudhary | director | marketing | 26/09/86 | 6700 # Write a shell script program to accept name, address and phone number & write it to a file. echo “ Enter file name : = >” read file1 touch $file1 if test –w $file1 then echo “Enter name, address and phone number of an employee” echo “Press ctrl+d to terminate….” Cat>$file1 else echo “file is not Writable : =>” fi Output Enter file name : =>nitin Enter name, address and phone number of an employee Press ctrl+d to terminate…. Rishi Dhontoli Nagpur 0712 6560274 //To display the contents of file Cat mal.sh Rishi

6

Dhontoli Nagpur 0712 6560274 Note: Tough the access and modification time of a file change when you try to read or write into it respectively. You may sometime require to set them to predefined values. Nested ifs: The if can be used upto nested level. Following is the example that uses three argument to take the pattern, as well as the input and output filenames. But first it checks whether the right number of argument have been entered. # Write a shell script program to check the given file is ordinary, special or directory. Clear echo “Enter the File Name :” read fn if test –f $fn then echo “File is a Ordinary file” else if test –d $fn then echo “File is a directory file” else if test –s $fn then echo “File is a special file” else echo “File does not exists” fi fi fi if… elif …else if also permits multi-way branching. In other words, you can evaluate more condition if the previous condition fails. For each condition that is evaluated, you can have a set of commands that which are executed if evaluation is found to be true. This form uses the elif keyword, and optionally it also uses the else statement in the same sense as the first form. It has the following syntax. Syntax: if then <execute command> elif then <execute command>

7

<…..> <…..> else <execute command> fi A single fi closes the construct, even though there may be a couple of elif statement for testing alternate conditions. It also resembles the else-if form used by other languages. $cat filetest.sh echo “Enter the Name of the file : \c” read flname if [ ! –f $flname ] then echo “File Does not Exist” elif [ ! –r $flname ] then echo “File is Not Readable” elif [ ! –w $flname ] then echo “File is Not Writable” else echo “File is both readable and Writable” fi $_ Output Test the script with two filenames – one that doesn’t exist and one that does: $ filetest.sh Enter the Name of the file : emp3.lst File Does not Exist $_ $ filetest.sh Enter the Name of the file : emp.lst File is Both readable and Writable. $_ A common mistake made by many, when using test for testing file attribute, is to use the shell’s wildcards as a substitute for file names. test doesn’t accept them at all, and uses only literal string. #Write a shell script program to find the entered number is odd or even. clear echo “Enter the Number :” read n echo “ ” if [ ‘expr $n % 2 –eq 0] then

8

echo “$n is Even” else echo “$n is Odd” fi Output: Enter the Number : 6 6 is even Enter the Number : 5 5 is Odd # Write a shell script program to find the greatest among three numbers.. Clear echo “ ” echo “Enter three numbers” read a b c echo “ ” if [ $a –gt $b –a $a –gt $b ] then echo “Grater number is $a” elif [$b –gt $a –a $b –gt $c ] then echo “Greater number is $b” else echo “greater number is $c” fi Output: Enter the Three Numbers 257 Greater number is 7 #Write a shell script to calculate the gross salary of employee with following conditions #a) if the basic sal > 5000 then hra=5% and da=3% #b) if the basic sal >10000 then hra=10% and DA = 3% echo “Enter the Basic Sal of employee:” read bs if [ $bs –gt 5000 ] then hra=’expr $bs \* 5 / 100’ echo “ ” echo hra = $hra

9

da = ‘expr $bs \* 3 / 100’ echo “ ” echo da = $da gross= ‘expr $bs + $da + $hra’ echo “ ” echo “GROSS SALARY = $gross’ elif [ $basic –gt 10000 ] then hra= ‘expr $basic \* 10 /100’ echo “ ” echo hra = $hra da = ‘expr $basic \* 8 / 100’ echo “ ” echo da = ‘$da’ gross = ‘expr $basic + $hra + $da’ echo “ ” echo “GROSS SALARY = $gross” fi Enter basic pay of employee : 7000 HRA = 350 DA = 210 GROSS SALARY = 7560 Looping with for: The for loop is different in structure from the ones used in other programming languages. There is no next statement here, neither can a step be specified. Unlike while and until, it doesn’t test a condition but uses a list instead. The syntax of this construct is as follows: Syntax: for variable in list do <execute commands> done The loop body is identical in structure to the while and until loops. The additional keywords here are variable and list. The list consist of a series of character strings, with each string separated from the other by white space. Each item in the list is assigned to the variable in turn and the loop body is executed. It performs the loop as many times as there are words in the list. A simple example can help you understand the things better: $ for x in 1 2 4 5 >do > echo “The value of x is $x”

10

>done Output: The value of x is 1 The value of x is 2 The value of x is 4 The value of x is 5 $_ #Write a shell script program to print the series. clear echo “Enter the Range” read n k=1 for((i=0;i<=$n;j++)) do for((j=1;j
done Output: Enter the range 5 1 22 333 4444 55555 Looping – The while Statement Loops lets you perform a set of instructions repeatedly. The shell features three types of loops – while, until and for. The first two are complementary to each other. All of them repeat the instructions set enclosed by certain keywords as often as the control command permits. The while statement should be quite similar to most programmers. It repeatedly performs a set of instructions till the control command returns a true status. The general syntax of this command is as follows: while do <execute commands> done The do and done are keywords. The set of instructions enclosed by do and done are to be performed as long as the condition remains true. Like in the if statement, this condition is actually the return value of a UNIX command or program. This means that you can use the test statement here also, with its associated expression, numeric and string comparisons and file. # write a shell program to print the first 10 numbers. clear echo “ ” i=1 while [ $i –le 10 ] do echo “Number $i” i= ‘expr $i + 1’ done echo “” Output: 1 2 3 4 5 6 7 8 9 10 #Write a shell script program to reverse the given number. clear echo “Enter any number :-” echo “ ”

12

read no sum=0 while [ $no –ne 0 ] do rem=’expr $no % 10’ sum=’expr $sum \* 10 + $rem’ no=’expr $no / 10’ done echo “ ” echo “Reverse number is = $sum” Output: Enter any number:123 Reverse number is = 6 # Write a shell script program to print the factorial of the given number. clear echo “ ” echo “Enter the number” read num fact=1 while test $num –ne 0 do fact=’expr $fact \* $num’ num=’expr $num – 1’ done echo “Factorial = $fact” Output: Enter the Number 5 Factorial = 120 # Write a shell script program to print Fibonacci series of a given number. clear echo “ ” a=0 b=1 echo “$a” echo “$b” echo “Enter the Number :” read n while [ $c –le n ] do

13

c=’expr $a + $b’ echo “$c” done Output: Enter the Number 5 1 1 2 3 5 8 while's Complement – The until Statement The until statement complements the while construct in the sense that the loop body here is executed repeatedly as long as the condition remains false. IT is simply another way of viewing the whole thing. Either can be used interchangeably, except for the fact that the expression has to be negated switching from one to another. until [ condition false ] do <execute commands> done # Write a shell script to print first 10 nos. clear echo “ ” i=1 until [ $i –gt 10 ] do echo “Number $i” i= ‘expr $i + 1’ done echo “ ” Output: 1 2 3 4 5 6 7 8 9 10 The case Statement The case statement is the second conditional offered by the shell . It doesn’t have parallel in most languages. dBASE and C use a similar form, though. The statement matches an expression for more than one alternative, and uses a compact construct to permit multi-way branching. It also handles string tests, but in a more efficient manner than if. The general syntax of the case statement:

14

Syntax: case <expression> in <pattern 1>) <execute commands>;; <pattern 1>) <execute commands>;; <pattern 1>) <execute commands>;; <….> <….> esca The keyword here are in and esac, and the symbols ;; are used as option terminator. The construct also uses the ) to delimit the pattern form the action. It matches the expression first pattern1, and is successful, execute the commands associated with it. If doesn’t, then it falls through and matches pattern2, and so on. Each command list is terminated by a pair of semicolons, and the entire construct is closed with an esac(reverse of case). Consider a simple example using this construct. You can device a script, which accept values from 1 to 5, and performs some action depending on the number keyed in. The code for this menu is stored in the file menu.sh. $ cat menu.sh cat <<END MENU 1. List of files 2. Processes of user 3. Today’s date 4. Users of system 5. Quit to UNIX END echo “Enter Your option: \c” read choice echo case “$choice” in 1) ls –l;; 2) ps –f;; 3) date ;; 4) who;; 5) exit;; esac $_ # Write a shell script program to check input character is digit, character of special symbol. clear echo “Enter any character :” read ch case $ch in [a-z]) echo “The character is in lower case” break;; [A-Z])

15

echo “The character is in UPPER case” break;; [0-9]) echo “The character is in digit” break;; *) echo “The character is in Special Character” break;; esac Output: Enter any character : A The character is in UPPER case Enter any character : a The character is in lower case Enter any character : 5 The character is in digit Enter any character : ^ The character is in Special Character The switch Statement Shell scripts are useful for providing menus to allows non-technical user to select commands or program from list. In menu shell script we would use commands in C – Language. The format of the switch command is Syntax: switch (string) case string1: . . breaksw case string2: . . breaksw default: . . breaksw end sw The switch command defines a character string if one of the case statements has a matching character string, that case statement is executed. If no case statement has a matching

16

string, the default statement is executed. When a case or default statement is executed, it executes a sequence, every statement between it and the following breaksw statement. foreach:foreach statement is used to control the flow of commands. foreach successively execute a group of statements, reading a new word from the specified variable at each execution. The format of the foreach shell command is: Syntax: foreach variable (wordlist) . . . . End The specified variable will be set to a word from “wordlist” on each execution of foreach. The foreach loop is terminated by an end command. When the last word has been used the shell executes the next command after the end command. Interrupt Handling (trap) We usually terminate a running program by pressing ctrl+d keys or using kill command or by pressing the delete key. All these methods sends certain signals to UNIX. We can change the effect of these signals to do something different from what they are originally meant to do. This is done by using the trap command. Some of these terminating signals and their corresponding signals-numbers. Signals and their corresponding Number: Number

Korn Shell Name

0

EXIT

This number does not correspond to a real signal, but the corresponding trap is executed before script termination.

1

HUP

(Ctrl+d) User Hang up

2

INT

The interrupt signal typically is generated using the DEL or the ^C key

3

QUIT

The quit signal is typically generated using the ^\ key. It is used like the INT signal but explicitly requests a core dump.

9

KILL

cannot be caught or ignored

Comment

The general format of the trap command is the following: trap “command.list” signal.list trap “command list1; command list2” 1 2 3 The command.list enclosed in double quotes, include the commands (separated by colon) that need to executed when UNIX receives any of the signals mentioned in the signal list. The

17

signal.list include the signal number (Separated by space) of the signals which we want to trap. The trap command is normally placed at the beginning of a program source-code. For example, to mention that execute the ls command whenever Ctrl +D signal (signal number 2) is received, we need to place the following command at the beginning of shell program. trap “ls” 2 The program in which the above command is placed, if interrupted in between by pressing ctrl+d keys, will display a list of files in the current directory rather than getting terminated. The following trap command will run the script exitscript and generate current-user listing on generation of signals 1,2 and 3. trap “exitscript;who” 1,2,3 The awk utility: The unix utility awk is a pattern matching. The UNIX utility awk is a pattern matching and processing language. It is used to search one or more specified files for record that match a specified pattern. If awk finds a match, the corresponding action is performed awk is a powerful search tool. It also performs such operations as omitting parts of a file, counting number of occurrence of a pattern in a file and writing reports. It is an excellent filter and report writer. Many UNIX utilities generates rows and columns of information. AWK is an excellent tool for processing these rows and columns, and is easier to use AWK than most conventional programming languages. It can be considered to be a pseudo-C interpretor, as it understands the same arithmatic operators as C. AWK also has string manipulation functions, so it can search for particular strings and modify the output. AWK also has associative arrays, which are incredible useful, and is a feature most computing languages lack. Associative arrays can make a complex problem a trivial exercise. A awk program is only a few lines long. This makes it more useful than users attaining the same purpose through traditional programming languages such as Pascal or C. The latter requires more though, more lines of code and hence more time. The awk programs are short because a large amount of detail is handled by the language itself automatically. The awk can manipulate large data-files in short (often-single line) program. Thus increasing productivity and efficiency. awk syntaxes are similar to those of ‘c’ language. Basic Structure The essential organization of an AWK program follows the form: pattern { action }

18

The pattern specifies when the action is performed. Like most UNIX utilities, AWK is line oriented. That is, the pattern specifies a test that is performed with each line read as input. If the condition is true, then the action is taken. The default pattern is something that matches every line. This is the blank or null pattern. Two other important patterns are specified by the keywords "BEGIN" and "END." As you might expect, these two words specify actions to be taken before any lines are read, and after the last line is read. The AWK program below: BEGIN END

{ print "START" } { print } { print "STOP" }

adds one line before and one line after the input file. This isn't very useful, but with a simple change, we can make this into a typical AWK program: BEGIN { print "File\tOwner"," } { print $8, "\t", $3} END { print " - DONE -" } The file is call as "FileOwner." But let's not put it into a script or file yet. I will cover that part in a bit. Hang on and follow with me so you get the flavor of AWK. The characters "\t" Indicates a tab character so the output lines up on even boundries. The "$8" and "$3" have a meaning similar to a shell script. Instead of the eighth and third argument, they mean the eighth and third field of the input line. You can think of a field as a column, and the action you specify operates on each line or row read in. There are two differences between AWK and a shell processing the characters within double quotes. AWK understands special characters follow the "\" character like "t". The Bourne and C UNIX shells do not. Also, unlike the shell (and PERL) AWK does not evaluate variables within strings. To explain, the second line could not be written like this: {print "$8\t$3" } That example would print "$8 $3." Inside the quotes, the dollar sign is not a special character. Outside, it corresponds to a field. What do I mean by the third and eight field? Consider the Solaris "/usr/bin/ls -l" command, which has eight columns of information. The System V version (Similar to the Linux version), "/usr/5bin/ls -l," has 9 columns. The third column is the owner, and the eighth (or nineth) column in the name of the file. This AWK program can be used to process the output of the "ls -l" command, printing out the filename, then the owner, for each file. I'll show you how. Update: On a linux system, change "$8" to "$9". One more point about the use of a dollar sign. In scripting languages like Perl and the various shells, a dollar sign means the word following is the name of the variable. Awk is different. The dollar sign means that we are refering to a field or column in the current line. When switching

19

between Perl and AWK you must remener that "$" has a different meaning. So the following piece of code prints two "fields" to standard out. The first field printed is the number "5", the second is the fifth field (or column) on the input line. BEGIN { x=5 } { print x, $x}

Executing an awk script So let's start writing our first AWK script. There are a couple of ways to do this. Assuming the first script is called "FileOwner," the invocation would be ls -l | FileOwner This might generate the following if there were only two files in the current directory: File Owner a.file barnett another.file barnett - DONE There are two problems with this script. Both problems are easy to fix, but I'll hold off on this until I cover the basics.

20

Related Documents

Shell Scripts
May 2020 3
Unix Shell Scripts
June 2020 8
Scripts
November 2019 27
Scripts
June 2020 14
Scripts
October 2019 32
Scripts
November 2019 27