Operating Systems Management, Lab, 11
Linux Opearating System
Lab 11
Instructions:
Use the VI editor to try the scripts below. Run the program and explain what the program is doing, unless you are asked to explain each line of code instead. DO NOT copy and paste the code into the editor. If you do so, the quotations will not be read correctly and cause issues.
- The example below is a while loop where the user controls when script ends. The second read statement within the while loop allows the user to set the value, which will be the new value tested in the while loop.
carryon=Y
while [ $carryon = “Y” ]
do
echo “This loop will continue as long as you type Y”
read carryon
done
echo “Since you type $carryon, and that is not Y, the loop has ended”
willmcg@willmcg:~$ vi lab11_1
willmcg@willmcg:~$ . lab11_1
This loop will continue as long as you type Y
Y
This loop will continue as long as you type Y
Y
This loop will continue as long as you type Y
e
Since you typed e, and that is not Y, the loop has ended
carryon=Y
while [ $carryon = "Y"]
do
echo "This loop will continue as long as you type Y"
read carryon
echo "Since you typed $carryon, and that is not Y, the loop has ended"
- The example below is a more traditional counter. The algorithm, counter=`expr $counter + 1` inside the loop, instructs the script to add 1 each time the loop executes. No user action is needed here, thus no read statement.
counter=0
while [ $counter -ne 10 ]
do
counter=`expr $counter + 1`
echo $counter
done
Explain what each line of code in the above program is doing
Line 1: setting the variable as counter and the value to 0 to start
line 2: it starts a while loop that loops the value of the variable that will continue the loop until it equals 10
Line 3: do marks the start of the loop body and continues each loop cycle till done
Line 4: setting the counter variable to add 1 each loop to itself
Line 5: echo displays the value of the variable because the variable has the $ command
Line 6: ends the body of the loop
willmcg@willmcg:~$ vi lab11_2
willmcg@willmcg:~$ . lab11_2
1
2
3
4
5
6
7
8
9
10
willmcg@willmcg:~$ . lab11_2
- Type the following:
counter=0
until [ $counter -eq 25 ]
do
counter=`expr $counter + 1`
echo $counter
done
willmcg@willmcg:~$ vi lab11_3
willmcg@willmcg:~$ . lab11_3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- Explain the difference between 2 & 3:
the main difference I see is the way the count ended. 2 was done by -ne not equals to and 3 was done with -eq equals to
The difference is that #2 uses
whileand #3 usesuntil. Awhileloop repeats as long as its condition is true. In #2, the condition iscounter -ne 10, so it keeps looping while the counter is not 10 and stops when it reaches 10. Anuntilloop repeats as long as its condition is false. In #3, the condition iscounter -eq 25, so it keeps looping while the counter is not 25 and stops when it reaches 25. In short,whilestops when its condition becomes false, whileuntilstops when its condition becomes true.*
- Type the code below into vi. Notice the first line: day=`date +%a` This line stores the output from the date command as the value for the variable day. This happens because of the use of the `Back Quotes` around the date command.
day=`date +%a` # Look up the Date command to see what this format does…
echo “Guess what day of the week it is today Use: Mon, Tue, Wed, Thu, Fri, Sat or Sun”
read guess
while [ $guess != $day ]
do
echo “wrong day, try again”
read guess
done
echo “Good Job, $guess is the right day!!!”
day=`date +%a` #Look up the Date Command to see what this format does...
echo "Guess what day of the week it is today Use: Mon, Tues, Wed, Thu, Fri, Sat or Sun"
read guess
while [ $guess !=$day ]
do
echo "wrong day, try again"
read guess
done
echo "Good Job, $guess is the right day!!!"
willmcg@willmcg:~$ . lab11_5
Guess what day of the week it is today Use: Mon, Tues, Wed, Thu, Fri, Sat or Sun
Mon
wrong day, try again
Tues
wrong day, try again
This
wrong day, try again
No
wrong day, try again
Sun
Good Job, Sun is the right day!!!
Type in and run the program. Then explain what each line of code is doing:
Line 1: sets the variable to day and its value as date with %a format specifier which prints the abbreviated weeday name, where as %A prints the full weekday name
Line 2: echos guess what day it is today and tells the programs thru use what to use as correct values
Line 3: collects the users guess
Line 4: while the guessed day is not today loop the prompt again
Line 5: starts the body of the loop
Line 6: echos the output if the day is not today
Line 7: reading the guess as correct
Line 8: ends the body of the loop
Line 9: outputs the corresponding statement to right day
- Try the following. Explain what happens:
x=0
while [ $x -ne 10 ]
do
echo $x
x=`expr $x + 1`
done
it goes from 0 - 9 counting up by 1 and doesn't include the 10 because the echo $x + 1 is after the echo $x
- Write a script that will count from 0 to 20 by 2’s.
x=0
while [ $x -ne 20 ]
do~~x=`expr $x+2`~~
echo $x
done
x=0
while [ "$x" ne 20 ]
do
x=`expr "$x" + 2`
done
willmcg@willmcg:~$ vi lab11_7
willmcg@willmcg:~$ . lab11_7
0+2
bash: [: 0+2: integer expected
willmcg@willmcg:~$ vi lab11_7
willmcg@willmcg:~$ . lab11_7
2
4
6
8
10
12
14
16
18
20
- Write a script that will count down from 100 to 0.
x=100
while [ "$x" -ne 0 ]
do~~x=`expr "$x" - 1`~~
done
x=100
while [ "$x" -ge 0 ]
do
echo "$x"
x=`expr "$x" - 1`
done
willmcg@willmcg:~$ vi lab11_8
willmcg@willmcg:~$ . lab11_8
expr: syntax error: unexpected argument '-1'
bash: [: : integer expected
willmcg@willmcg:~$ vi lab11_8
willmcg@willmcg:~$ . lab11_8
willmcg@willmcg:~$ vi lab11_8
willmcg@willmcg:~$ . lab11_8
willmcg@willmcg:~$ vi lab11_8
willmcg@willmcg:~$ . lab11_8
100
99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
- Write a script that will count to 243 multiplying by 3.
x=0
while [ "$x" -ge 243 ]
do
echo "$x"~~x`expr "$x" \* 3`~~
done0 * anything is 0
x=1
while [ "$x" -le 243 ]
do
echo "$x"
x=`expr "$x" \* 3`
done
x=1
while [ "$x" -le 243 ]
do
echo "$x"
x=`expr "$x" \* 3`
done
willmcg@willmcg:~$ vi lab11_9
willmcg@willmcg:~$ . lab11_9
1
3
9
27
81
243
willmcg@willmcg:~$ vi lab11_9