View
8
Download
4
Tags:
Embed Size (px)
Certification
bash Shell Scripting
Copyright 2003 Red Hat, Inc.
1
Rev RH033-RHEL3-1
UNIT 16
bash Shell Scripting
Copyright 2003 Red Hat, Inc.
2
Rev RH033-RHEL3-1
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.
Copyright 2003 Red Hat, Inc.
3
Rev RH033-RHEL3-1
UNIT 16: Agenda
Shell scripting
Copyright 2003 Red Hat, Inc.
4
Rev RH033-RHEL3-1
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
Copyright 2003 Red Hat, Inc.
5
Rev RH033-RHEL3-1
Creating Shell Scripts
Step 1: Use a text editor such as vi to create a text file containing commands
First line contains the magic shbangsequence: #!#!/bin/bash
Comment your scripts! Comments start with a #
Copyright 2003 Red Hat, Inc.
6
Rev RH033-RHEL3-1
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 -OR- Specify the absolute or relative path to the
script on the command line
Copyright 2003 Red Hat, Inc.
7
Rev RH033-RHEL3-1
Generating Output
Use echo to generate simple outputecho 'Welcome to Red Hat Linux paradise!'
echo -n "Please enter the file name: "
Use printf to generate formatted outputprintf "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.
Copyright 2003 Red Hat, Inc.
8
Rev RH033-RHEL3-1
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
Copyright 2003 Red Hat, Inc.
9
Rev RH033-RHEL3-1
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
Copyright 2003 Red Hat, Inc.
10
Rev RH033-RHEL3-1
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.
Copyright 2003 Red Hat, Inc.
11
Rev RH033-RHEL3-1
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 $?
Copyright 2003 Red Hat, Inc.
12
Rev RH033-RHEL3-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
Copyright 2003 Red Hat, Inc.
13
Rev RH033-RHEL3-1
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 until
Copyright 2003 Red Hat, Inc.
14
Rev RH033-RHEL3-1
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!'
Copyright 2003 Red Hat, Inc.
15
Rev RH033-RHEL3-1
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
Copyright 2003 Red Hat, Inc.
16
Rev RH033-RHEL3-1
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
executableif [ -f $HOME/lib/functions ];then
source ~/lib/functions
fi
Copyright 2003 Red Hat, Inc.
17
Rev RH033-RHEL3-1
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
Copyright 2003 Red Hat, Inc.
18
Rev RH033-RHEL3-1
Selection StructuresUsing 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 ]; then
do something
else
do something else
fi
Copyright 2003 Red Hat, Inc.
19
Rev RH033-RHEL3-1
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
Copyright 2003 Red Hat, Inc.
20
Rev RH033-RHEL3-1
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
docommands...
done
Copyright 2003 Red Hat, Inc.
21
Rev RH033-RHEL3-1
Selection StructuresThe 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
Copyright 2003 Red Hat, Inc.
22
Rev RH033-RHEL3-1
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
break stops processing the loop entirely, jumping past the done statement
exit exits from the shell script entirely You may provide an exit status
Copyright 2003 Red Hat, Inc.
23
Rev RH033-RHEL3-1
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
Copyright 2003 Red Hat, Inc.
24
Rev RH033-RHEL3-1
End of Unit 16
Questions and answers Summary