1. CIS 216 Unix Scripting
Highline Community College
Dan Morrill
2. When working with case you want to read input:
read choice
cd $choice
This reads the input from the keyboard and then changes the
directory over to the one specified by the user. Choice is
read, and then used as a variable
You can do this anywhere in a script for just about any
thing you can do on a Linux Box
read UserProcess
ps –ef |grep $UserProcess
3. 1. FRUIT="kiwi"
2. case "$FRUIT" in
3. "apple") echo "Apple pie is quite tasty."
4. ;;
5. "banana") echo "I like banana nut bread."
6. ;;
7. "kiwi") echo "New Zealand is famous for kiwi."
8. ;;
9. esac
4. In this program we see Fruit is set = “kiwi”
The case statement reads the $Fruit and where there is
a match, in this case kiwi, outputs the text:
New Zealand is famous for kiwi
If we wanted to change this so that user input was
considered we would change:
Read Fruit
echo “allowed entries are apple, banana, and kiwi”
remember case sensitivity.
5. choiceA || continue
If no conversion needs to be done, a continue
statement restarts execution of the loop.
the “||” = to a logical OR
[ “$choiceA] or continue
6. The tput utility uses the terminfo database to make
the values of terminal-dependent capabilities and
information available to the shell, to initialize or reset
the terminal, or return the long name of the requested
terminal type. tput outputs a string if the attribute
(capability name) is of type string, or an integer if the
attribute is of type integer. If the attribute is of type
boolean, tput simply sets the exit code (0 for TRUE if
the terminal has the capability, 1 for FALSE if it does
not), and produces no output.
7. 1. case $RCOUNT in
2. 1) tput cup 2 0; echo -e "- L. bc"
3. sleep $INTERVAL
4. ;;
Case using a counting variable $RCOUNT
TPUT CUP (Position the cursor on the screen to XY 2
and 0 echo the letter L
Sleep for the variable $INTERVAL
8. To change the color of the text, use the setb option for setting the
background color and the setf option for setting the foreground
color along with the number of the color assigned in the
terminfo database. The following numbers and colors are
typically assigned but may vary from each UNIX system:
0: Black
1: Blue
2: Green
3: Cyan
4: Red
5: Magenta
6: Yellow
7: White
9. tput cup 2 0; echo -e " Lo.. bc“
To
tput setf 4 cup 2 0; echo -e " Lo.. bc“
To have a foreground color of Red
setf = foreground color
setb = background color
This is bad for people who are color blind and not using
the standard terminal coloring
10. 1. INTERVAL=1000 # Sleep time between rotation intervals
2. RCOUNT="0" # For each RCOUNT the line rotates 1/8
3. # cycle
4. while : # Loop forever...until this function is killed
5. do
6. (( RCOUNT = RCOUNT + 1 )) # Increment the RCOUNT
7. case $RCOUNT in
8. 1) tput cup 2 0; echo -e "- L. bc"
9. sleep $INTERVAL
10. ;;
11. 2) tput cup 2 0; echo -e " Lo.. bc"
12. sleep $INTERVAL
13. ;;