Unix - Shell Scripting

96
06/06/22 07:53 AM 5864_ER_FED_ALT 1 Unix Shell Programming - Advanced Vinodh K Nair

description

Unix Shell Scripting Information.

Transcript of Unix - Shell Scripting

Page 1: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 1

Unix Shell Programming - Advanced

Vinodh K Nair

Page 2: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 2

Introduction:

• Name

• Short ID

• Role

• Responsibility

• Experience

• Area of interest

• Any previous experience in shell scripting

• Current rating on shell scripting knowledge (1-10)

• Expectations from the program?

Page 3: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 3

Ground Rules

In order to ensure the productivity of our training, we will need to be….

Pagers and mobile phones off

Full participation

One speaker at a time

Respect the views of others

Silence indicates agreement

Keep to the break times agreed

Page 4: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 4

Course Agenda:

Shell Basics

1. Introduction to Shell and types of Shells 2. Variables and Keywords 3. Metacharacters 4. Regular Expressions 5. Using grep and egrep 6. Using Quotes

Introduction Shell Programming

1. Shell Programming – Step 1 2. Expression Handling 3. Positional Parameters 4. Conditional Statement 5. Case Statement 6. Loops in Shell 7. IO Redirection

Advance Shell Programming

1. Shell Functions 2. Advanced tools a) The find utility b) Cutting the outputs with cut 3. Fundamentals of awk 4. Fundamentals of sed 5. Miscellaneous tools 6. Debugging the Shell scripts 7. Dealing with Signals

Page 5: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 5

Pre Quiz

• Write your name and Employee ID without fail.

• 25 questions, multiple choices – in 15 minutes

• Use the pre-quiz columns for writing your answers

• Do not discuss

• A question can have more than one correct answer, but choose the best one.

• Please encircle your guessed answers to find how good you are at guessing…

• Chocolates will be distributed for:

1. Pre-quiz highest scorer

2. Post-quiz highest scorer

3. One with maximum difference

Page 6: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 6

Unix Basics

Structure of Unix

H/W

Kernel

Shell

Applications

Page 7: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 7

Shell

Shell is a command interpreter and acts as an interface between a user and kernel

Types of Shells

Bourne Shell

/usr/bin/sh

Korn Shell

/usr/bin/ksh

C Shell

/usr/bin/csh

bash

/bin/sh

POSIX

/usr/bin/sh

Page 8: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 8

Page 9: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 9

HPUX Directory Structure

/ (root)

stand bin lib dev usr tmp opt etc home

bin lib newconfig

user1 user2 user3 user4

sbin

Page 10: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 10

Unix Shell Programming

BREAK

Page 11: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 11

Permissions

- - - - - - - - -

owner group others

Permission Description Weight

r read 4

w write 2

x execute 1

Ex:

- rw- r- -r- - 1 user1 group1 12 Oct 10:45 file1

Use chmod to change the attributes

Page 12: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 12

Absolute path

Absolute path represents the location of a file or directory starting from the root directory

Ex:

/> /home/user1/scripts/check.sh

Relative path

Accessing files and directories from the current directory

Ex:

/home/user/scripts> ./check.sh

Page 13: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 13

VI Editor

bottom

Page 14: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 14

Page 15: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 15

Shell Basics

Variables

Variables are the general words which are used to store and manipulate information within a shell program. Variables are fully under user control. These can be created and destroyed at anytime.

Ex: <Variable_name> = <Variable_Value>

TOWN=Delhi ; TOTAL=0 ; DATE= 28Nov05

– Local Variables

Local Variables are the variables that are presented within the current instance of the Shell. These are not available to the any child processes that are started by the current shell. Local variables are defined by users.

– Global Variables

Global Variables are the Variables that are presented within the current shell and also available to any child processes that are started by the current shell.

Page 16: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 16

Keywords

Keywords are the words which have standard predefined meaning in the shell already. Keywords can not be used as variable names. Keywords are also called as Reserve words.

– List of Keywords

echo read set unset readonly shift

export if else fi while do

done for until case esac break

continue exit return trap wait exec

sleep test

Page 17: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 17

Environment Variables

Environment Variables are the special variables that are set by the shell and is required by the shell in order to function correctly. These variables are also called as shell variables.

– List of Environment Variables

PATH Defines the path which the shell must search in

HOME Stores the default working directory for the user

LOGNAME Stores the login name of the user

SHELL Defines the name of default working shell

TERM Defines the name of the terminal

PWD Stores the Present working Directory

PS1 Defines the system prompt

IFS Defines the Internal Field Separator

Page 18: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 18

Unix Shell Programming

BREAK

Page 19: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 19

Shell Meta Characters

. Matches any one character

* Represents any combination of any characters

? Represents any one character

[…] Matches any one character from the enclosed list

[!...] Matches any one character except those in the list

Ex:

$ ls a* Lists all files beginning with character a

$ ls a?b? Lists all 4 character filenames whose first character is ‘a’ and third character is ‘b’

$ ls [aeiou]* Lists all the files whose first character is a, e, i, o, u

$ ls [A-Fv-z]* Lists all files whose first character is in the range A to F or v to z

$ ls [!A-Z]* Lists all files whose first character is anything other than in the range of A to Z

Page 20: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 20

Regular Expressions

A regular expression is a string that can be used to describe several sequences of characters.

^ Matches the beginning of a line

$ Matches the end of a line

\ Used to specify patterns that contains wild cards

^$ Matches a blank line

\< Matches at the beginning of the word

\> Matches at the end of the word

\<\> Matches a complete word

Page 21: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 21

Basic Shell commands

• man - Very important command known in UNIX

• cp [-ir…] file1 file2

cp [-ir…] file-list directory– i for interactive. prompt use whenever a file will be overwritten

– r for recursive. copy a directory tree

• ls [-alRF…] file-list– a for listing all files including the dot files

– l for long format

– R for recrusive. list the all subdirectories.

– F for listing directories with a trailing /

• date [+format]– %date ‘+%h %d, 19%y’

Oct 1, 1996

Page 22: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 22

Basic Shell commands (Cont…)

• wc file-list Display the number of lines, words and characters

• more file-list Browse through text files one page at a time.

• head [-n …] file-listDisplay the first n lines of the files (default=10)

• tail [+n|-n| -f| …]– Display the last few lines in the files (default = 10)– Example:

# tail +5 foo # display the last parf of foo starting from line 5

# tail -5 foo # display the last five lines of foo

# tail +30 foo | head -15 | more #display line 30-45 of foo

# tail -f foo # wait and display the new lines appended to foo

Page 23: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 23

Basic Shell commands (Cont…)

• cut -c list file

cut -f list [-dChar] file– Cut out selected charatcers or fields from each line of a file

– Examples:– # cut -c 1-5,15-20 foo – # extract chars 1-5 and 15-20 from each line of

foo.– # cut -f 1,3 -d” “ moo # extract field 1 and 3 from each line of moo.

• paste file1 file2

– Concatenate corresponding lines of the given input files

– Example (reverse two fields of the file abc)

# cut -f1 abc > abc1

# cut -f2 abc > abc2

# paste abc2 abc1 > xyz

Page 24: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 24

Basic Shell commands (Cont…)

sort [-tC…] [-o outfile] [field-list] [file-list]

sort the files– # sort +1 list1 # sort list 1 starting from field 2 to the end of the line– # sort +2 -3 list2 # sort list2 on the third field– # sort -n -o list4 list3 # sort list3 numerically and place the output in list4

diff file1 file 2

– Display different lines that are found when comparing two files– It prints a message that users ed-line notation (a - append, c - change, d -delete)

to describe how a group of lines has changed.– It also describes what changes need to be made to the first file to make it the

same as the second file.– Example

file1 file2 file3

apples apples oranges

oranges oranges bananas

bananas kumquats peaches

Page 25: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 25

Basic Shell commands (Cont…)

Tr Command

– Translate input character to output character based on the input and output patterns

– Example

# tr ‘[A-Z]’ ‘[a-z]’ <in >out

# translate all letters to lower case.

# tr -s ‘\012\011\040’ ‘\012\012\012’ < in > out

# translate blank, tab and new line chars to new line chars and squeeze (-s) consecutive newline char into one

# tr -cs ‘[a-z][A-Z]’ ‘[\012*]’ < in > out

# change all non-alphabetic (-c) chars to new line chars and squeeze consecutive new line char into one.

# tr -d ‘\040’ < in > out

# delete all blanks.

Page 26: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 26

Unix Shell Programming

BREAK

Page 27: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 27

Basic Shell commands (Cont…)

Uniq

– Display a file, removing all successive repeated lines

– Example:

file1: # uniq file1

apple apple

banana banana

banana apple

apple banana

banana

# sort fruit | uniq -c

apple 2

banana 3

# tr -cs ‘[a-z][A-Z]’ ‘[\012*]’ < fileA | sort | uniq

# show a list of distinct words in fileA.

Page 28: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 28

Basic Shell commands (Cont…)

Find – Recursively search the directory tree rooted at <pathname> and find all files

whose names satisfy <exp>

– There are many details in the expression.

– Examples:

# find . -name \*.doc -print # list all files names ending with .doc

# find /etc/source -atime 2 -print

# print the names of the files under /etc/source whose lst access time was 2 days ago.

# find . -name “[a-z]*” -exec rm {} \;

# remove the files under the current directory whose names begin with a lower case letter.

# find / \(-name a.out -o -name “*.o” \) -atime +7 -exec {} \;

# remove the object and binary executable files under the root directory which have not be accessed more than 7 days.

Page 29: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 29

Basic Shell commands (Cont…)

• du [-as….] [dir list] [file list]

– Reports the allocated dispace for each file and/or directory specified

– -a lists all files, -s lists the grand total of each dir given

– Examples:

# du -s

print the total disk space used by the files in and under the current dir.

# du -s *

print the disk space used by each file and dir in the current dir.

Page 30: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 30

The grep

The grep stands for globally regular expression print. It lets us to search particular patterns in a file. The general form of grep is shown below…

grep < word > < file_name >

– Grep options

- i Ignores the case

- n Lists the line numbers along with matching patters

- v Lists all the lines that do not match the patters

- l Lists only filenames that contain match

Ex:

$ grep ^unix read.txt Lists the lines beginning with word ‘unix’

$ grep system$ read.txt Lists the lines ending with word ‘system’

$ grep -l hello * Lists the file name which contains word ‘hello’

$ grep -v unix read.txt Lists all the lines except those which contains the word ‘unix’

Page 31: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 31

Quotes

\ Used to specify patterns that contains wild cards

‘ ‘ Used to take string as-is

“ “ Used when command substitution is needed

` ` Used when command evaluation is needed

Ex:

$ echo Hello\;Welcome

$ echo ‘$PWD’

$ echo “$PWD”

$ echo “Working Directory is : `pwd`”

$ OS=‘HP-UX’

$ VER=’11i’

$ echo ‘$OS $VER’

$ echo ‘$OS $VER `hostname`’

$ echo “$OS $VER”

$ echo “$OS $VER `hostname`”

Page 32: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 32

Unix Shell Programming

BREAK

Page 33: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 33

What is a shell script?

• A text file containing UNIX commands

• A text file with READ and EXECUTE permissions

• A program executed/interpreted by the shell

• A tool for automating UNIX tasks

• A program that doesn’t need to be compiled

• A program that is easy to create and modify

Page 34: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 34

Introduction to Shell Programming

Shell Programming – Step1

A shell program can be defined as a series of commands to be executed to obtain a desired results. It can also be called as a shell script.

Structure of a script

Shell Initialization

Script Description

Variable Declaration

Function Definition

Main Program

Function calling

Exit with Return Status

Page 35: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 35

Planning to write a shell script:

Planning:

What is the script required to do? What program components are required in the script? What logical testing conditions are required? Will user input be required? Will any special variables be required?

Preparation:

Organize the components into logical order Keep everything simple to start with Write the components in plain English, and it to shell syntax

Page 36: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 36

Guidelines for writing Unix Commands/Tools/Scripts

• Standard command format

• Recognize meta-characters (handle multiple files)

• Standard I/O (stdin,stdout, stderr. If file arg is absent use std)

• Keep messages and prompts to a minimum.

• Provide verbose options

• Input/output data should be text whenever possible.

• Use dot files and environment variables for frequently used info.

• Use standard library and tools to save coding effort.

Page 37: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 37

Simple Program

Consider the following script and observe the output

### Script #1 welcome.sh

echo “Hello! Welcome to SHELL World “

echo “Shell Scripts are very handy to system admins…”

echo “Enjoy the Course”

$ chmod u+x welcome.sh

$ ./welcome.sh

Hello! Welcome to SHELL World

Shell Scripts are very handy to system admins…

Enjoy the Course

Page 38: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 38

Interactive Script

Script that runs interactively. It takes input from user while execution

###Script #2 interactive.sh

echo “Enter your Name :”

read name

echo “Enter your Designation :”

read design

echo “Enter your Organization name :”

read org

echo “Hello $name!!”

echo “Your Position in $org is $design”

Page 39: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 39

Here Document

Here Document provides STDIN to Unix command from lines that follow until delimiter is found at start line.

General syntax

command << delimiter

input command 1

input command 2

… … …

delimiter

Ex:

$ ./interactive.sh << EOF

Paul

System Administrator

CSC

EOF

Page 40: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 40

Example:Clear the screen, move the cursor to line 10, column 20 and turn on bold.

tput -S <<EOF clear cup 10 20 bold EOF

Reset text attributes to normal without clearing screen #tput sgr0

Page 41: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 41

Unix Shell Programming

BREAK

Page 42: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 42

Page 43: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 43

Shell Arithmetic

expr command is used to perform integer arithmetic operations in shell.

General syntax

expr < integer 1 > < operand > < integer 2 >

– expr operands

Operand Operations

+ Addition

- Subtraction

\* Multiplication

/ Division

% Modulus

Ex:

$ TOTAL=`expr 5 + 4 \* 2`

Page 44: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 44

Page 45: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 45

Arithmetic Substitution (ksh and bash)

In ksh and bash, we can perform arithmetic without using expr command

General syntax

$(( expression ))

– Arithmetic substitution Operators

Operator Operation

+ Addition

- Subtraction

* Multiplication

/ Division

( ) Group the expression to be evaluated

Ex:

$ VAL=$(( (( 5+3*2) -4)/2))

Page 46: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 46

Page 47: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 47

Page 48: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 48

Escape Sequence

An escape sequence is a special sequence of characters that represents another character. It is used to format the output messages.

Escape Sequence Description

\a Beep sound

\b Backspace

\c Suppress Trailing new line

\f Formfeed

\n New line

\r Carriage Return

\t Tab

\\ Backslash

Ex:

$ echo “First Tab\tSecond Tab”

$ echo “First Line\nSecond Line”

Page 49: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 49

Positional Parameters

These are the special variables defined by the shell and are numbered $1 thorough $9. These can be used to pass the additional information to the script as arguments. Values can be set to these variables using set command.

Ex:

$ set Shell is a command interpreter

$ echo $1 $2 $3 $4 $5

Variable Description

$0 Name of the command being executed

$n Variable correspond to the argument ( n is b/w 1-9 )

$# No of arguments passed

$* Lists all the arguments passed

$@ Lists all the arguments passed

$? Exit status of the previous command

$$ Process ID of the current Shell

$! Process ID of the last background command

Page 50: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 50

Unix Shell Programming

BREAK

Page 51: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 51

Conditional StatementConditional statement is used to implement decision control instruction.

Shell uses the keyword ‘if’ for this purpose.

General syntax

1. if <test_condition>

then

command set

fi

2. if <test_condition>

then

command set1

else

command set2

fi

__________________________________________________________________________

EXAMPLES:

if test $# -eq 0 if [ $# -eq 0 ]

then then

echo “no positional param!” echo “no positional param!”

fi fi

Page 52: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 52

Page 53: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 53

Conditional test operators

Numerical Test String TestOperator Description Operator Description

-gt Greater than str1 = str2 True if strings are same-lt Less than str1 != str2 True if strings are different-ge Greater or equal -n str True if length of string > 0

-le Less than or equal -z str True if length of string is 0-ne Not equal str True if string is not null-eq Equal

Logical Connectives

Operator Description ! Condition is False

-a Logical AND -o Logical OR

File Test Operator Description

-f True if file exists and is not a directory-s True if file exists and size is greater than 0-d True if file exists and is a directory-r True if file exists and has a read permission-w True if file exists and has a write permission-x True if file exists and has an execute permission

Page 54: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 54

if [ ! condition ] If condition is not true if [ -f $FILENAME ] If file exists called $FILENAME if [ -d $DIRNAME ] If directory exists called $DIRNAME if [ -x $FILENAME ] If file $FILENAME is executable if [ -w $FILENAME ] If file $FILENAME is writable if [ -r $FILENAME ] If file $FILENAME is readable if [ -s $FILENAME ] If file $FILENAME exists and is of greater than zero size. if [ $VAR1 -ne $VAR2 ] If VAR1 does not equal VAR2 if [ $VAR1 -gt $VAR2 ] If VAR1 is greater than VAR2 if [ $VAR1 -lt $VAR2 ] If VAR1 is less than VAR2 if [ $VAR1 -eq $VAR2 ] If VAR1 is equal to VAR2 if [ $VAR1 -ge $VAR2 ] If VAR1 is greater than or equal to VAR2 if [ $VAR1 -le $VAR2 ] if VAR1 is less than or equal to VAR2 if ["$VAR1"="$VAR2"] If VAR1 is equal to VAR2 (for strings) if ["$VAR1"!="$VAR2"] If VAR1 does not equal VAR2 (strings) if [ ! "$VAR1" ] If VAR1 is null if [ "$VAR1" ] If VAR1 is not null

Page 55: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 55

Unix Shell Programming

BREAK

Page 56: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 56

The case statement

The case statement in shell is used for flow control. It allows us to select one out of multiple choices available. It avoids nesting if-else-fi statements.

General syntax

case value in

choice1) command choice 1

;;

choice2) command choice 2

;;

choice3) command choice 3

;;

*) command choice 4

to execute for no match

;;

esac

Page 57: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 57

###Script #3 makechoice.sh

echo “Hello!! Welcome to the address tool”

echo “1. CSC Indore”

echo “2. CSC Noida”

echo “3. CSC Hyderabad”

echo “\n Choice: \c”

read choice

case $ choice in

1) echo “CSC Indore”

echo “Building #, Indore, MP”

;;

2) echo “CSC Noida”

echo “C-24/25, Sector- 56, Noida, UP”

;;

3) echo “CSC Hyderabad”

echo “Building 7, Mindspace, Hyderabad, AP”

;;

*) echo “Invalid choice selected”

;;

esac

Page 58: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 58

Loops

Loops enable us to execute series of commands multiple times. There are various loops available in shell to perform the repeated execution.

The while loop The until loop The for loop

While and until loops works in a very similar fashion. While loop executes as long as the test condition remains true whereas the until loop executes as long as the test condition remains false.

For loop works slightly different from the while and until. It executes set of commands repeatedly for each item listed in the list. The most common use of for loop is to perform same set of commands for large number of files.

Page 59: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 59

The while loop

General syntax

while <test condition>

do

command set

done

###Script #4 printnum1.sh

i=0

while [ $i –lt 10 ]

do

echo $i

i=`expr $i + 1`

done

Page 60: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 60

Page 61: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 61

The until loop

General syntax

until <test condition>

do

command set

done

###Script #5 printnum2.sh

i=10

until [ $i –eq 0 ]

do

echo $i

i=`expr $i - 1`

done

Page 62: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 62

Page 63: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 63

The for loop

General syntax

for item in item1 item2 item3 … … …

do

command set

done

### Script #6 printnum3.sh

for num in 1 2 3 4 5 6 7 8 9

do

echo $num

done

Page 64: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 64

Page 65: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 65

Unix Shell Programming

BREAK

Page 66: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 66

IO redirection

When a command is executed, the result produced is printed to a terminal which is called STDOUT. And the error messages occurred are printed to a special terminal called STDERR. Most of the commands will have STDOUT as their STDERR.

Using the IO redirection operators, we can redirect the output or error messages to any files instead of STDOUT or STDERR. All these IO devices are identified by numbers as follows…

0 Standard In 1 Standard Out 2 Standard Error 2>&1 Redirect standard error to standard out &> Redirect standard error & standard out < Redirect input > Redirect output >> Concatenate output to file << Here Is File for scripts | Pipe

Page 67: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 67

Sleep, return, break, continue & exit statements

Sleep:

The sleep command is used to suspend execution for a certain amount of time. You provide the number of seconds to suspend as the argument to sleep command.

Return:

The return command is used to return control from a called subroutine to the instruction following the call to that subroutine.

Break:

The break command discontinues the execution of loop immediately and transfers control to the command following the done keyword.

Continue:

The continue command skips the remaining part of the loop and transfers control to the start of the loop for next iteration.

Exit:

The exit command completely terminates the program. It returns an exit code that is optionally provided as argument in the program.

Page 68: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 68

Functions

A subroutine (function, method, procedure, or subprogram) is a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code. The syntax of POSIX shell includes support for creating self contained subroutines, and for calling and returning from them.

There are many advantages to breaking a program up into subroutines, including:

• reducing the duplication of code in a program (e.g., by replicating useful functionality, such as mathematical functions),

• enabling reuse of code across multiple programs,

• decomposing complex problems into simpler pieces (this improves maintainability and ease of extension),

• improving readability of a program,

• hiding or regulating part of the program

The components of a subroutine may include:

• a body of code to be executed when the subroutine is called

• parameters that are passed to the subroutine from the point where it is called

• a value that is returned to the point where the call occurs

Page 69: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 69

Page 70: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 70

Page 71: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 71

Page 72: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 72

Page 73: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 73

Page 74: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 74

Unix Shell Programming

BREAK

Page 75: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 75

Page 76: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 76

Page 77: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 77

Page 78: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 78

AWK

The Awk text-processing language is useful for such tasks as:

• Tallying information from text files and creating reports from the results.

• Adding additional functions to text editors like "vi".

• Translating files from one format to another.

• Creating small databases.

• Performing mathematical operations on files of numeric data.

C:\Vinod\Documents\docs\awk.txt

Page 79: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 79

Page 80: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 80

Page 81: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 81

Page 82: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 82

Page 83: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 83

Page 84: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 84

Unix Shell Programming

BREAK

Page 85: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 85

Page 86: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 86

Page 87: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 87

Page 88: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 88

Page 89: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 89

SED

Page 90: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 90

Debug

Sometimes the hardest part about shell programming is finding the bugs in your scripts. This session walks you through the various features of the shell along with some other techniques to help you track down and fix the bugs in your scripts.

Syntax:

/bin/sh <options> <script_name>

Options:

-x Shell tracing option

-v Verbose option

-n Syntax checking

Example:

/bin/sh -xv my_script

/bin/sh -n my_script

Page 91: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 91

Exercise

Page 92: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 92

Exercise

• Search exact pattern from /etc/passwd

• Add 10 users using script

• Move a list of *.txt to *.doc

• Mirror (lvextend) 10 lvols using a script

• Remove all blank lines from a file

• Remove 10 different lines from a file matching patterns

• Investigate a given command used by a set of users

• Write a script to add two values supplied as script arguments

Page 93: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 93

Post Quiz

• Write your name and Employee ID without fail.

• 25 questions, multiple choices – in 15 minutes

• Use the post-quiz columns for writing your answers

• Do not discuss

• A question can have more than one correct answer, but choose the best one.

• Please encircle your guessed answers to find how good you are at guessing…

• Chocolates will be distributed for:

1. Pre-quiz highest scorer

2. Post-quiz highest scorer

3. One with maximum difference

Page 94: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 94

Sl no Trainee Name Pre-quiz Post-quiz Difference

1        

2        

3        

4        

5        

6        

7        

8        

9        

10        

11        

12        

13        

14        

15        

16        

17        

18        

19        

20        

Score Card

Page 95: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 95

My Contact:

Vinodh Kombissan [email protected] Mobile: 9940041817 Nortel : +91 44 39878899 Extn 1227

Computer Sciences Corporation,

6th & 7th Floor , No 9 Prince Kushal Towers,

Chennai - 600 002.

Please provide your valuable feedback and suggestions…

Page 96: Unix - Shell Scripting

04/07/23 10:21 PM 5864_ER_FED_ALT 96

Thank You