You are on page 1of 25

Certification

bash Shell Scripting

UNIT 16
bash Shell Scripting

Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

UNIT 16: Objectives


Learn why shell scripting is useful.
Learn how to create a basic shell script.
Learn how to generate output and read
input.
Learn how to use flow control to write
more powerful shell scripts.
2

Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

UNIT 16: Agenda


Shell scripting

Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

Scripting Basics
Shell scripts are text files that contain a
series of commands or statements to be
executed.
Shell scripts are useful for:
Automating commonly used commands
Performing system administration and
troubleshooting
Creating simple applications
Manipulation of text or files
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

Creating Shell Scripts


Step 1: Use a text editor such as vi to
create a text file containing commands
First line contains the magic shbang
sequence: #!
#!/bin/bash

Comment your scripts!


Comments start with a #
5

Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

Creating Shell Scripts cont.


Step 2: Make the script executable:
$ chmod a+x myscript.sh

To execute the new script:


Place the script file in a directory in the
executable path -ORSpecify the absolute or relative path to the
script on the command line
6

Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

Generating Output
Use echo to generate simple output
echo 'Welcome to Red Hat Linux paradise!'
echo -n "Please enter the file name: "

Use printf to generate formatted output


printf "The result is %0.2f \n" $RESULT

Syntax similar to C printf() function


Does not automatically put a newline at the end of the
output.
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

Handling Input
Use read to assign an input value to a
shell variable:
echo -n "Enter the filename: "
read FILENAME
read reads from standard input and assigns

one word to each variable


Any leftover words are assigned to the last
variable
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

Using Positional Parameters


Positional parameters are special
variables that hold the command-line
arguments to the script.
The positional parameters available are
$1 , $2 , $3 , etc. . These are normally
assigned to more meaningful variable
names to improve clarity.
$* holds all command-line arguments
9

Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

Using functions in shell scripts


Shell scripts may include shell functions.
Shell functions may improve program
readability. They also help to remove
repetitious code from the scripts.
Shell functions must be declared before
they are used.
1
0
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

Using functions, continued


Arguments may be passed to a shell
function by using their own set of
positional parameters ( $1, $2 etc. )
myFunction $filename

The value of $filename will be available as


$1 inside the body of myFunction

Functions may return values by using the


'return' keyword which sets the value of
the special variable $?
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

1
1

Exit Status
Commands exit with an exit status
0 for success, 1 to 255 for failure
Exit status of most recently executed
command is kept in the $? variable just
like return values from shell functions

Shell scripts may set an exit status


with the exit command:
exit 1 # Indicates an error
1
2
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

Control Structures
The three types in shell programming :
Sequential structures - the program flows
one line after another
Selection structures - code execution based
on a logical decision
if, if/else , if/elif/else and

conditional operators

Repetition structures - code execution is


repeated based on a logical decision
for, while and unti l
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

1
3

Conditional Execution
Commands may be executed
conditionally, based on the exit status of
the previous command
&&
||

logical AND
logical OR

Examples:
$ grep joe passwd || echo 'No joe!'
$ cp -a /tmp/*.o . && echo 'Done!'
1
4
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

Selection Structures:
Using the if Statement
if selection structures execute the body

of the structure only if the condition


tested is true
if [ condition ]; then
do something
fi
1
5
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

File Tests
File tests:
-f tests to see if file exists and is a

regular
file
-d tests to see if a file exists and is a
directory
-x tests to see if a file exists and is
executable
if [ -f $HOME/lib/functions ];then
source ~/lib/functions
1
6

fi
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

String Tests
Strings may be tested as well
-z returns true if the string is empty
-n returns true if the string is not empty

operators such as =, != , < and > may be


used to compare strings as well
if [ $(id -u) = "0" ]; then
echo "You are logged in as root"
fi
Rev RH033-RHEL3-1

1
7
Copyright 2003 Red Hat, Inc.

Selection Structures
Using if/else Statements
if/else selection structures execute
the body of the if structure only if the

condition tested is true, otherwise the


else is executed
if [ condition ] ; t h e n
do something
else
do something else
fi
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

1
8

Selection Structures:
Using the case Statement
The case statement provides an
alternative method for performing
selections that may be cleaner than
multiple if/elif/else tests
case variable in

pattern1)

do something ;;

pattern2)

do another thing ;;
esac
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

1
9

Repetition Structures:
The for-loop
The for repetition structure provides a
method for iterating, or looping, through
a list of values and executing commands
on each of these values.
for variable in list- of- values
do
commands...
done
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

2
0

Selection Structures
The while-loop
The while loop structure provides a
useful method for performing a set of
commands while a condition remains
true. The syntax is:
while condition
do
commands...
done
2
1
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

continue and break


while and until loops can be

disrupted during execution


continue stops the current execution of

the loop and reexamines the initial


condition, possibly restarting the loop
b r e a k stops processing the loop
entirely, jumping past the done
statement
exit exits from the shell script entirely
You may provide an exit status
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

2
2

Shell script debugging


In order to debug a shell script invoke the
shell interpreter with debug options or
change the shebang to include the
debug options
bash -x scriptname
bash -v scriptname
#!/bin/bash - x
#!/bin/bash - v
2
3
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.

End of Unit 16
Questions and answers
Summary

2
4
Rev RH033-RHEL3-1

Copyright 2003 Red Hat, Inc.