Understanding UNIX CASE and TPUT
-
Upload
dan-morrill -
Category
Education
-
view
146 -
download
0
description
Transcript of Understanding UNIX CASE and TPUT
CIS 216 Unix Scripting
Highline Community College
Dan Morrill
Understanding Case and TPUT
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 Boxread UserProcessps –ef |grep $UserProcess
Before Case comes Read
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
Case with a default entry
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 Fruitecho “allowed entries are apple, banana, and
kiwi” remember case sensitivity.
Case with a default entry
choiceA || continueIf no conversion needs to be done, a continue statement restarts execution of the loop.the “||” = to a logical OR[ “$choiceA] or continue
Continue Statement
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.
TPUT
1. case $RCOUNT in2. 1) tput cup 2 0; echo -e "- L. \b\c"3. sleep $INTERVAL4. ;;
Case using a counting variable $RCOUNTTPUT CUP (Position the cursor on the screen
to XY 2 and 0 echo the letter LSleep for the variable $INTERVAL
TPUT in action
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: Black1: Blue2: Green3: Cyan4: Red5: Magenta6: Yellow7: White
TPUT also does Colors
tput cup 2 0; echo -e "\\ Lo.. \b\c“Totput setf 4 cup 2 0; echo -e "\\ Lo.. \b\c“
To have a foreground color of Redsetf = foreground colorsetb = background color
This is bad for people who are color blind and not using the standard terminal coloring
So you could change
1. INTERVAL=1000 # Sleep time between rotation intervals2. RCOUNT="0" # For each RCOUNT the line rotates 1/83. # cycle
4. while : # Loop forever...until this function is killed5. do6. (( RCOUNT = RCOUNT + 1 )) # Increment the RCOUNT
7. case $RCOUNT in8. 1) tput cup 2 0; echo -e "- L. \b\c"9. sleep $INTERVAL10. ;;11. 2) tput cup 2 0; echo -e "\\ Lo.. \b\c"12. sleep $INTERVAL13. ;;
Putting Interval, Count, TPUT together
Questions?