Tutorial 12 - Shell Scripting - Part 2
Content under development
Contents
ADDITIONAL SHELL SCRIPTING
Main Objectives of this Practice Tutorial
- Use the if-elif-else control flow statement in a shell script.
- Use the for loop control using a list with command substitution.
- Use the while loop in a shell script.
- Use the exit and break statements in a shell script.
- Explain how to configure and use a .bashrc start-up file.
Tutorial Reference Material
Course Notes |
Linux Command/Shortcut Reference |
YouTube Videos | ||
Course Notes:
|
Control Flow Statements
Additional Statements |
Startup Files
|
Brauer Instructional Videos: |
KEY CONCEPTS
IF-ELIF-ELSE STATEMENT
The elif statement can be used to perform additional conditional tests of the previous test condition tests FALSE. This statement is used to make your logic control-flow statement to be more adaptable.
How it Works:
If the test condition returns a TRUE value, then the Linux Commands between
then and else statements are executed.
If the test returns a FALSE value, then a new condition is tested again,
and action is taken if the result is TRUE, then the Linux Commands between
then and else statements are executed. Additional elif statements can be used if additional conditional testing is required . Eventually, an action will be taken
when the final test condition is FALSE.
Example:
num1=5
num2=10
if test $num1 –lt $num2
then
echo “Less Than”
elif test $num1 –gt $num2
then
echo “Greater Than”
else echo “Equal to”
fi
FOR LOOP USING COMMAND SUBSTITUTION
Let’s issue the for loop with a list using command substitution.
In the example below, we will use command substitution to issue the ls command and
have that output (filenames) become arguments for the for loop.
Example:
for x in $(ls)
do
echo “The item is: $x”
done
WHILE LOOP
The while loop is useful to loop based on the result from a test condition or command result.
This type of loop is very useful for error-checking.
How it Works:
The condition/expression is evaluated, and if the condition/expression is TRUE,
the code within … the block is executed.
This repeats until the condition/expression becomes FALSE.
Reference: https://en.wikipedia.org/wiki/While_loop
Example 1:
answer=10
read –p “pick a number between 1 and 10: “ guess
while test $guess –eq 10
do read –p “Try again: “ guess
done
echo “You are correct”
Example 2:
value=1
while [ $value -le 5 ]
do
echo "$value"
((value=value+1)) # could also use ((value++))
done
1
2
3
4
5
EXIT & BREAK STATEMENTS
exit Statement
The exit statement is used to terminate a shell script.
This statement is very useful when combined with logic in a shell script.
The exit command can contain an argument to provide the exit status of your shell script.
Example:
if [ $# -ne 1 ]
then
echo "USAGE: $0 [arg]"
exit 1
fi
break Statement
The break statement is used to terminate a loop.
Although the loop terminates, the shell script will continue running.
Example:
read -p "Enter a number: " number
while [ $number -ne 5 ]
do
read -p "Try again. Enter a number: " number
if [ $number -eq 5 ]
then
break
fi
done
START-UP FILES
Shell configuration (start-up) files are scripts that are run when you log in, log out, or start a new shell.
The start-up files can be used, for example, to set the prompt and screen display, create local variables,
or create temporary Linux commands (aliases)
The /etc/profile file belongs to the root user and is the first start-up file that executes when you log in, regardless of shell.
User-specific config start-up files are in the user's home directory:
- ~/.bash_profile runs when you log in.
- The ~/.bashrc runs when you start an interactive Bash shell.
Logout Files
There is a file that resets or restores the shell environment (including shut-down of programs running in the shell) when the user logs out of their shell. User-specific logout start-up files are in the user's home directory: ~/.bash_logout