Understanding UNIX CASE and TPUTPresentation Transcript
CIS 216 Unix ScriptingHighline Community College Dan Morrill
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
1. FRUIT="kiwi"2. case "$FRUIT" in3. "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
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.
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
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.
1. case $RCOUNT in2. 1) tput cup 2 0; echo -e "- L. bc"3. sleep $INTERVAL4. ;; 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
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
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
1. INTERVAL=1000 # Sleep time between rotation intervals2. RCOUNT="0" # For each RCOUNT the line rotates 1/83. # cycle4. while : # Loop forever...until this function is killed5. do6. (( RCOUNT = RCOUNT + 1 )) # Increment the RCOUNT7. case $RCOUNT in8. 1) tput cup 2 0; echo -e "- L. bc"9. sleep $INTERVAL10. ;;11. 2) tput cup 2 0; echo -e " Lo.. bc"12. sleep $INTERVAL13. ;;