< Previous | Contents | Next >
while
bash can express a similar idea. Let’s say we wanted to display five numbers in sequen-
tial order from one to five. a bash script could be constructed as follows:
#!/bin/bash
# while-count: display a series of numbers count=1
while [[ $count -le 5 ]]; do echo $count count=$((count + 1))
done
echo "Finished."
#!/bin/bash
# while-count: display a series of numbers count=1
while [[ $count -le 5 ]]; do echo $count count=$((count + 1))
done
echo "Finished."
When executed, this script displays the following:
[me@linuxbox ~]$ while-count
1
2
3
4
5
Finished.
[me@linuxbox ~]$ while-count
1
2
3
4
5
Finished.
The syntax of the while command is:
while commands; do commands; done
Like if, while evaluates the exit status of a list of commands. As long as the exit status is zero, it performs the commands inside the loop. In the script above, the variable count is created and assigned an initial value of 1. The while command evaluates the exit status of the [[]] compound command. As long as the [[]] command returns an exit status of zero, the commands within the loop are executed. At the end of each cycle, the [[]] command is repeated. After five iterations of the loop, the value of count has increased to 6, the [[]] command no longer returns an exit status of zero and the loop terminates. The program continues with the next statement following the loop.
We can use a while loop to improve the read-menu program from the previous chapter:
#!/bin/bash
# while-menu: a menu driven system information program
#!/bin/bash
# while-menu: a menu driven system information program
DELAY=3 # Number of seconds to display results
while [[ $REPLY != 0 ]]; do
clear
cat <<- _EOF_
Please Select:
1. Display System Information
2. Display Disk Space
3. Display Home Space Utilization
0. Quit
_EOF_
read -p "Enter selection [0-3] > "
if [[ $REPLY =~ ^[0-3]$ ]]; then if [[ $REPLY == 1 ]]; then
echo "Hostname: $HOSTNAME" uptime
sleep $DELAY
fi
if [[ $REPLY == 2 ]]; then df -h
sleep $DELAY
fi
if [[ $REPLY == 3 ]]; then
if [[ $(id -u) -eq 0 ]]; then
echo "Home Space Utilization (All Users)" du -sh /home/*
else
echo "Home Space Utilization ($USER)" du -sh $HOME
fi
sleep $DELAY
fi
else
echo "Invalid entry."
sleep $DELAY
fi
done
echo "Program terminated."
DELAY=3 # Number of seconds to display results
while [[ $REPLY != 0 ]]; do
clear
cat <<- _EOF_
Please Select:
1. Display System Information
2. Display Disk Space
3. Display Home Space Utilization
0. Quit
_EOF_
read -p "Enter selection [0-3] > "
if [[ $REPLY =~ ^[0-3]$ ]]; then if [[ $REPLY == 1 ]]; then
echo "Hostname: $HOSTNAME" uptime
sleep $DELAY
fi
if [[ $REPLY == 2 ]]; then df -h
sleep $DELAY
fi
if [[ $REPLY == 3 ]]; then
if [[ $(id -u) -eq 0 ]]; then
echo "Home Space Utilization (All Users)" du -sh /home/*
else
echo "Home Space Utilization ($USER)" du -sh $HOME
fi
sleep $DELAY
fi
else
echo "Invalid entry."
sleep $DELAY
fi
done
echo "Program terminated."
By enclosing the menu in a while loop, we are able to have the program repeat the menu display after each selection. The loop continues as long as REPLY is not equal to “0” and the menu is displayed again, giving the user the opportunity to make another selection. At the end of each action, a sleep command is executed so the program will pause for a few seconds to allow the results of the selection to be seen before the screen is cleared and the menu is redisplayed. Once REPLY is equal to “0,” indicating the “quit” selection,
the loop terminates and execution continues with the line following done.