Understanding UNIX CASE and TPUT

11
CIS 216 Unix Scripting Highline Community College Dan Morrill Understanding Case and TPUT

description

Understanding Case statements, user input, and TPUT

Transcript of Understanding UNIX CASE and TPUT

Page 1: Understanding UNIX CASE and TPUT

CIS 216 Unix Scripting

Highline Community College

Dan Morrill

Understanding Case and TPUT

Page 2: Understanding UNIX 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

Page 3: Understanding UNIX CASE and TPUT

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

Page 4: Understanding UNIX CASE and TPUT

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

Page 5: Understanding UNIX CASE and TPUT

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

Page 6: Understanding UNIX CASE and TPUT

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

Page 7: Understanding UNIX CASE and 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

Page 8: Understanding UNIX CASE and TPUT

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

Page 9: Understanding UNIX CASE and TPUT

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

Page 10: Understanding UNIX CASE and TPUT

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

Page 11: Understanding UNIX CASE and TPUT

Questions?