Bash Inter
Bash Inter
User interfaces
1
User Interfaces
• Often scripts do multiple tasks
• Important to give clear instructions and options to the user
• Even with command line programs, UI can be organized.
2
tput
barr@Comp390-WG1:~/classes/comp190/scripts$ ui1.sh Hello World!
#!/bin/bash
# Print message in center of terminal
cols=$( tput cols )
rows=$( tput lines )
message=$@
input_length=${#message}
half_input_length=$(( $input_length / 2 ))
middle_row=$(( $rows / 2 ))
middle_col=$(( ($cols / 2) - $half_input_length ))
tput clear
tput cup $middle_row $middle_col
tput bold
echo $@
tput sgr0
tput cup $( tput lines ) 0 All the command line messages are put into the center of the display
3
• Line 4 - tput cols will tell us how many columns the
1 #!/bin/bash terminal has.
2 # Print message in center of terminal • Line 5 - tput lines will tell us how many lines (or
3 rows) the terminal has.
4 cols=$( tput cols ) • Line 7 - Take all the command line arguments and assign
5 rows=$( tput lines ) them to a single variable message.
6 • Line 9 - Find out how many characters are in the
7 message=$@ string message. We had to assign all the input values to
the variable message first as ${#@} would tell us how
8 many command line arguments there were instead of
9 input_length=${#message} the number of characters combined.
10 • Line 11 - We need to know what 1/2 the length of the
string message is in order to center it.
11 half_input_length=$(( $input_length / 2 ))
12 • Lines 13 and 14 - Calculate where to place the message
for it to be centered.
13 middle_row=$(( $rows / 2 ))
• Line 16 - tput clear will clear the terminal.
14 middle_col=$(( ($cols / 2) - $half_input_length ))
15 • Line 18 - tput cup will place the cursor at the given
row and column.
16 tput clear
17 • Line 19 - tput bold will make everything printed to
the screen bold.
18 tput cup $middle_row $middle_col
• Line 20 - Now we have everything set up let's print our
19 tput bold message.
20 echo $@ • Line 21 - tput sgr0 will turn bold off (and any other
21 tput sgr0 changes we may have made).
22 tput cup $( tput lines ) 0 • Line 22 - Place the prompt at the bottom of the screen.
4
tput
• See this link for tput options
http://linuxcommand.org/lc3_adv_tput.php
5
Example 1 ui2.sh
#!/bin/bash
# term_size2 - Dynamically display terminal window size
redraw() { Function redraw. Clears the screen and displays the width and height.
clear
echo "Width = $(tput cols) Height = $(tput lines)"
}
Capname Description
sc Save the cursor position
rc Restore the cursor position
home Move the cursor to upper left corner (0,0)
cup <row> <col> Move the cursor to position row, col
cud1 Move the cursor down 1 line
cuu1 Move the cursor up 1 line
civis Set to cursor to be invisible
cnorm Set the cursor to its normal state
7
Example 2 ui3.sh
#!/bin/bash
# term_size3 - Dynamically display terminal window size with text centering
redraw() {
local str width height length
9
Text Effects example
#!/bin/bash
# tput_characters - Test various character attributes
clear ui4.sh
echo "tput character test"
echo "==================="
echo
tput bold; echo "This text has the bold attribute."; tput sgr0
tput smul; echo "This text is underlined (smul)."; tput rmul
# Most terminal emulators do not support blinking text (though xterm
# does) because blinking text is considered to be in bad taste ;-)
tput blink; echo "This text is blinking (blink)."; tput sgr0
tput rev; echo "This text has the reverse attribute"; tput sgr0
# Standout mode is reverse on many terminals, bold on others.
tput smso; echo "This text is in standout mode (smso)."; tput rmso
tput sgr0
echo
10
Text color
• Most terminals support 8 foreground text colors and 8 background colors (though some support as many as 256).
• Using the setaf and setab capabilities, we can set the foreground and background colors.
• The exact rendering of colors is a little hard to predict. Many desktop managers impose "system colors" on terminal windows, thereby modifying
foreground and background colors from the standard. Despite this, here are what the colors should be:
Value Color
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White
8 Not used
9 Reset to default color
11
Text color example ui5.sh
#!/bin/bash
12
Clearing the screen
• These capnames allow us to selectively clear portions of the terminal
display:
Capname Description
smcup Save screen contents
rmcup Restore screen contents
el Clear from the cursor to the end of the line
el1 Clear from the cursor to the beginning of the line
ed Clear from the cursor to the end of the screen
clear Clear the entire screen and home the cursor
13
Clearing the screen example ui6.sh
#!/bin/bash
# tput_menu: a menu driven system information program
Please Select: The _EOF_ …text…_EOF_ is called a “here document” or “heredocs”. It basically demarcates a string.
1. Display Hostname and Uptime
14
Clearing the screen example
read -p "Enter selection [0-3] > " selection # -p means display prompt; read into variable "selection"
# Clear area beneath menu
tput cup 10 0 Cup = move cursor
echo -n ${BG_BLACK}${FG_GREEN}
tput ed
Echo will execute the commands in the variables and change screen colors
tput cup 11 0 # Act on selection
ed = clear from cursor to end of screen
case $selection in
1) echo "Hostname: $HOSTNAME" ;;
0) break
uptime
;;
;; *) echo "Invalid entry." * Matches any number
2) df -h ;;
;; esac End of case statement
3) if [[ $(id -u) -eq 0 ]]; then
printf "\n\nPress any key to continue."
echo "Home Space Utilization (All Users)" read -n 1
du -sh /home/* 2> /dev/null done If no variable name specified to
else read, it reads into $REPLY
# Restore screen
echo "Home Space Utilization ($USER)"
tput rmcup Restore screen
du -s $HOME/* 2> /dev/null | sort -nr echo "Program terminated."
fi contents
15
tput
• See this link for tput options
http://linuxcommand.org/lc3_adv_tput.php
16