LINUX - SCRIPT - IF - and - Looping - IA - Changed 2
LINUX - SCRIPT - IF - and - Looping - IA - Changed 2
Linux Review
Echoline Script
This script requests the user to enter a line of text and then repeats it back word by word.
In the echo statement,
-e is used to enable the interpretation of backslash-escaped characters including the
\c option which is to suppress a trailing newline
Notice that the words in the sentence are broken out into 5 variables (w1, w2, w3, w4,
and w5). Also notice that when these variables are used in the echo statement they have a
1
Introduction To Enterprise Servers
Information Systems Module: LINUX Scripts- Introduction
$ in front of them so that the value of the variables is displayed and not the name of the
variables.
jpico [return]
^X
Name the file echoline
chmod 700 echoline
sh echoline [return]
Type This is a test of my script. [return]
Run it again and enter another longer sentence.
How could you change the script to repeat more words?
If statements have a condition portion, what to do if the condition is true and what to do if
the condition is false. They also have a scope terminator – something to tell the script
that the if statements has ended.
The condition portion is written in a variety of ways, but is typically comparing one thing
against another in terms of equality or less than or greater than. Symbols used include
= for equal to, -eq for equal to, -lt for less than, -gt for greater than,
-le for less than or equal to, -ge for greater than or equal to.
See man expr for more discussion of expressions in Linux.
2
Introduction To Enterprise Servers
Information Systems Module: LINUX Scripts- Introduction
if condition
then
what to do if the condition is true
else
what to do if the condition is false
fi
The word “then” is required for the “if” statement. The word “else” is optional.
fi is required and ends the “if” statement.
In the example below, the read statement is used with the –p option. This is the option
that specifies that a prompt will be used. The text in quotes "Guess my favorite number:
" is the prompt. When the user enters a number, that becomes the value of the variable
num. As in the previous example, to tell the shell to use the value of the variable num, it
is used with a $ in front of it.
Number Example:
echo "Welcome to the guess my favorite number game"
read -p "Guess my favorite number: " num
if [ $num = 7 ]
then
echo $num "is my favorite number too!"
echo "You win the prize!"
else
echo "Although not my favorite,"
echo "the" $num "is a cool number too!"
fi
uoas050@elinux:~> sh number
Welcome to the guess my favorite number game
Guess my favorite number: 7
7 is my favorite number too!
You win the prize!
uoas050@elinux:~> sh number
Welcome to the guess my favorite number game
Guess my favorite number: 9
Although not my favorite,
the 9 is a cool number too!
3
Introduction To Enterprise Servers
Information Systems Module: LINUX Scripts- Introduction
Fruit Example:
In the fruit example, the user is asked to enter their favorite fruit. Notice the if statement
above in which the favorite number is not in quotes. Compare that to the if statement
below in which the favorite fruit is in quotes. This is because the fruit is not numeric
data.
4
Introduction To Enterprise Servers
Information Systems Module: LINUX Scripts- Introduction
Run the script twice – once with apple as the fruit and once with another fruit as the fruit:
uoas050@elinux:~> sh fruit
Dave's Fruit Stand
Enter your favorite fruit: apple
The apple is my favorite fruit too!
Free groceries for you for the day!
uoas050@elinux:~> sh fruit
Dave's Fruit Stand
Enter your favorite fruit: orange
Although not my favorite,
the orange is good too!
5
Introduction To Enterprise Servers
Information Systems Module: LINUX Scripts- Introduction
fi
^X
Name the file moveex
Run the script twice. If it worked correctly the first time, why doesn’t it work correctly
the second time?
uoas050@elinux:~> sh moveex
Move completed successfully - file moved.
Status 0
uoas050@elinux:~> ls –l
uoas050@elinux:~> sh moveex
mv: cannot stat `october': No such file or directory
Move completed unsuccessfully - file not
moved. Status 1
6
Introduction To Enterprise Servers
Information Systems Module: LINUX Scripts- Introduction
Copy the file bonus to bonus2 and change the script to give a bonus when the calls
handled are greater than or equal to 150 or if their calls are greater than or equal to 50.
Copy the file bonus to bonus3 and change the script to give a bonus when BOTH
conditions are met.
Run the new script with the same three scenarios. Did you get the response you should?
Retire Example:
Write a script to determine if someone can retire. If the years of service is not less than
25, then they can retire, otherwise, a message is printed telling them they need 25 years
of service to retire.
read –p “Years of Service: “ Years
if [[ ! $Years –lt 25 ]]
then
echo “You can retire now.”
else
echo “You will need 25 years to retire.”
fi
^X
Name the file retire
Run the script and respond 32. Run it again and respond 21.
ELSE IF
ElseIF - Using the elif Clause in an if Statement:
Generic syntax:
if list
then
statements
elif list
then
statements
else
statements
fi
Example: Revisit the Guess my number script and give messages specifying if the
number guessed is greater than or less than the favorite number if the number is not
guessed. First, copy the number script to numberif and then edit it to something similar
to the following:
7
Introduction To Enterprise Servers
Information Systems Module: LINUX Scripts- Introduction
Run the script at least three times – once supplying the actual number, once supplying a
number greater than the number and once supplying a number less than the number. Do
you get the expected results?
Example: Create a script that indicates whether a company has achieved one of three
scenarios – profit, loss, or breakeven.
8
Introduction To Enterprise Servers
Information Systems Module: LINUX Scripts- Introduction
uoas050@elinux:~> sh proflossbe
Enter Sales Amount: 5000
Enter Costs: 6500
Loss of: -1500
uoas050@elinux:~> sh proflossbe
Enter Sales Amount: 6500
Enter Costs: 6500
Profit and Costs are equal - breakeven.
uoas050@elinux:~> sh proflossbe
Enter Sales Amount: 6500
Enter Costs: 5000
Profit of: 1500
Nesting IF Statements
In this example, a person is eligible to retire if their age is at least 60 with 10 years of
service OR if they have 25 years of service regardless of age. Think about this one
before entering the script. Sample results are included below:
uoas050@elinux:~> sh retirementstatus
Enter Age: 50
Enter Years of Service:10
In order to retire, you must be at least age
60
with 10 Years of Service or
have 25 years of service.
uoas050@elinux:~> sh retirementstatus
Enter Age: 62
Enter Years of Service:26
You may retire!
uoas050@elinux:~> sh retirementstatus
Enter Age: 15
Enter Years of Service:20
Your age is less than your Years of Service! This is
invalid.
Create the retirementstatus script and run with 50 for Age and 10 for Years of Service.
Run again with 62 for age and 26 for Years of Service.
Run again with 15 for age and 20 for Years of Service.
9
Introduction To Enterprise Servers
Information Systems Module: LINUX Scripts- Introduction
10
Introduction To Enterprise Servers
Information Systems Module: LINUX Scripts- Introduction
;;
esac
save the script as colorcase. Run it twice – once with the color blue and once with the
color red as the response. The output is below.
uoas050@elinux:~> sh colorcase
Enter color: blue
the color is not red - it is blue
uoas050@elinux:~> sh colorcase
Enter color: red
The color is red
For a pattern, you can use characters or numbers. You can also use the pattern-matching
symbols of * for matching all characters, ? for matching a single character, or […] for
matching a range of characters can be used.
Suppose you want to allow red, Red, or RED to be valid input in response to the question
to enter a color. To do so, you would need to use the vertical bar | to separate the patterns
that are possible.
Copy the file and change it to accept red, Red, and RED as the color red.
Save the script as colorcase2. Run it four times with the following responses: Red, RED,
red, and blue. The output is shown below.
uoas050@elinux:~> sh colorcase2
11
Introduction To Enterprise Servers
Information Systems Module: LINUX Scripts- Introduction
uoas050@elinux:~> sh colorcase2
Enter color: RED
The color is red
uoas050@elinux:~> sh colorcase2
Enter color: red
The color is red
uoas050@elinux:~> sh colorcase2
Enter color: blue
the color is not red - it is blue
12
Introduction To Enterprise Servers
Information Systems Module: LINUX Scripts- Introduction
#The following code assigns a random number to a variable called temp and
#then performs a loop up to 32767 times or until another random number is
#equal to the original random number stored in temp.
Note—the RANDOM function uses a default seed value of 1—produces a pseudo set
of random numbers that are always the same with a seed of 1. Use the format,
RANDOM(seed) to get other streams of random numbers. For example, try
RANDOM(3333).
((temp = $RANDOM))
((i = 0))
while [[ $i -lt 32767 ]]
do
((rn = $RANDOM))
((i++))
echo 'i= ' $i ' The random number is: ' $rn
if [[ $temp = $rn ]]
then
echo 'The original random number was: ' $temp
break
fi
done
echo 'The original random number was: ' $temp
Example 2:
13
Introduction To Enterprise Servers
Information Systems Module: LINUX Scripts- Introduction
Instead of limiting the number of times the loop is executed, the following code stops
only when the first random number (assigned to the variable temp) is repeated.
((i = 0))
((rn = 0))
((temp = $RANDOM))
while [[ $temp -ne $rn ]]
do
((rn = $RANDOM))
((i++))
echo 'i= ' $i ' The random number is: ' $rn
done
echo 'The original random number was: ' $temp
14