Download presentation
Presentation is loading. Please wait.
Published byColeen Randall Modified over 6 years ago
1
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
if [ condition ] … fi Loop statements for x in arg1 arg2 argN do … done Termination statements exit 0 Creating Portable Bash Shell Scripts She-bang line ( #! )
2
Advanced Shell Scripting
In the previous week, we learned how to place Unix/Linux OS commands into a file called a shell script. This week, we will focus on creating bash shell scripts that use logic and looping. Using logic and loops allow us to create more flexible shell scripts that will behave differently to changing inputs.
3
Using Logic As we mentioned earlier, we may want our shell script to behave differently based on different data inputted when using the read command. There are several logic statements available in Unix/Linux, but we will just concentrate on this course with the if statement.
4
Using Logic The purpose of the if statement is to test a condition and execute a command (or commands) if the condition tests as true. If the condition tests as false, then the commands are ignored (or skipped). if [ condition ] then command 1 command 2 command N fi The condition is tested in square brackets [ ]. If the condition is true, THEN command 1 thru to command N are executed. If the condition is false, then the commands are skipped. fi (opposite of the word “if”) is used to end the if statement.
5
Using Logic Methods to Test a Condition:
Testing Text: var=“Murray Saul” if [ $var = “Murray Saul” ] then echo “match” fi Notice: a space must exist between the condition and the square brackets!
6
Using Logic Methods to Test a Condition:
Testing Values: Numerical Comparison Operator Purpose -eq Equal to -gt Greater than -ge Greater than or equal to -lt Less than -le Less than or equal to -ne Not equal to
7
Using Logic Methods to Test a Condition:
Testing Values: String Comparison Operator Purpose = Equal to != Not equal to
8
Using Logic Example # Shell script to determine retirement age
printf “Enter your Age: ” read age if [ $age –ge 65 ] then echo “Congratulations, you can retire! ” fi
9
Using Logic Additional Methods to Test a Condition:
Testing Files: Option Purpose -f Regular file exists -d Directory file exists -s File has non-zero size (i.e. not empty) -r File is readable (i.e read permissions) -w File is writable (i.e. write permissions) -x File is executable (i.e. execute permissions) Using exclamation mark ! Is used for not Eg. [ ! –r filename ] (testing if filename is not readable!)
10
Using Logic Example # Shell script to check if file exists
printf “Please enter a regular file pathname: “ read pathname if [ -f $pathname ] then echo “The file at the pathname $pathname exists!” fi
11
Using Loops Loops allow us to create shell script that can perform repetitive tasks – for examples repeat a task for every file in the current directory. When combining loops with logic, our shell script can become a very useful and a very powerful tool.
12
Using Loops The for loop is used when the number of repetitions are known. There are other loops such as the while or until loops that are used when the number of repetitions are not know and the repetition is based on testing a condition For the purpose of this course, we will only look at the for loop where the number of repetitions are known.
13
Using Loops The for loop can be used in different ways. For the purpose of this course, we will demonstrate how to use a for loop that repeats for specific arguments used within the for loop. Each argument is stored in the variable (in this case called “x”) and is executed through one complete cycle of the loop. When there are no further arguments to be used, the for loop ends. The do and done commands mark the cycle of the loop to be executed. The variable $x can be used in these commands… For x in arg1 arg2 … argN do command 1 command 2 command N done
14
Using Logic Example arg1 arg2 arg3
# Shell script to loop several arguments for filename in /etc/passwd /etc/foo /etc/group do if [ -f $filename ] then echo “The regular file $filename exists!” fi done arg1 arg2 arg3
15
The exit statement Sometimes you may want to terminate or stop the execute of your shell script. The exit command is used to terminate executing shell scripts. The exit command is particularly useful when used with loops and logic. Terminates the shell script and provides a true or successful exit status to the operating system. Terminates the shell script and provides a false or unsuccessful exit status to the operating system. Any number other than zero or “true”… exit 0 exit 1 The variable $? Can be used to display the exit status of a command (eg. if successful) or a shell script (eg. Did it exit sucessfully?)
16
Using Logic Example # Shell script to check if file exists
for y in file1 file2 file3 file4 do if [ ! -r $y ] then echo “The files require read permission for owner!” exit 1 fi ls –l $y done
17
Creating Portable Bash Shell Scripts
There are many shells that are available for users to use in both Unix and Linux. Many of these shells such as the bash, Korn, C-shell, etc. contain common commands, but syntax for performing more complex tasks (like mathematics) may require different techniques between the shells
18
Creating Portable Bash Shell Scripts
Question: What if someone took a shell script designed to run in a specific shell and ran it in the wrong shell? Answer: It could generate errors, or the entire script may not run correctly (or not run at all)!
19
Creating Portable Bash Shell Scripts
Solution: Have a comment line that runs the shell script in a specified shell. If that shell is not available, do not run the shell script! This comment line is referred to as the “she-bang” line for the beginning characters #!
20
Creating Portable Bash Shell Scripts
Sample: She-bang line must be in first line of shell script, and #! Must be first two characters on the line. If anything else, line will be treated like a regular comment. #!/usr/bin/bash # Shell script to loop # several arguments for filename in a b c do if [ -f $filename ] then echo “$filename - exists!” fi done Following the #! The correct absolute pathname of the shell must be specified. If there is a typo, then shell script will not run! Use the which or whereis command to determine the pathname of the bash shell
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.