|
CS469 - Linux and Unix Administration and Networking
| Displaying exercises/e5/solution/b.sh
#!/bin/bash
# Prompt the user for a start/end numbers and print all the odd numbers between
# them. If start > end, switch start and end.
# Example output:
# > ./b.sh
# Start? 0
# End? 20
# 1 3 5 7 9 11 13 15 17 19
# Use read to prompt for a start and ending value:
read -p "Start? " start;
read -p " End? " end;
# If the start > end, switch the two:
if (( start > end )); then
let t=start;
let start=end;
let end=t;
fi
# If start is an even number, increment by one:
if (( start%2 == 0 )); then let start++; fi
# Use a C style for loop to loop through the odd numbers until start > end.
# Increment start by two each time through the loop. Use echo to print the
# number followed by a space, but without a newline.
for (( ; start <end; start+=2 )) {
echo -n $start " ";
}
# After the loop is finished use echo to print a newline.
echo ;
|