In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use tput command in Linux, the article is very detailed, has a certain reference value, interested friends must read it!
The Linux common command tput initializes and manipulates your terminal session through the terminfo database. By using tput, you can change several terminal functions, such as moving or changing the cursor, changing text properties, and clearing specific areas of the terminal screen.
tput initializes and manipulates terminal sessions via terminfo database
What is a terminfo database? The terminfo database on UNIX systems defines the properties and functions of terminals and printers, including the number of rows and columns for each device (for example, terminals and printers) and the properties of text to be sent to that device. Several popular programs in UNIX rely on the terminfo database to provide these properties and many others, including the vi and emacs editors and the curses and man programs.
Like most commands in UNIX, the tput command can be used either on the shell command line or in shell scripts. To give you a better understanding of tput, this article starts at the command line and then follows with shell script examples.
Cursor Properties
Moving the cursor or changing cursor properties can be useful in UNIX shell scripts or on the command line. In some cases, you may need to enter sensitive information, such as a password, or enter information in two different areas on the screen. In such cases, using tput may be helpful.
tput clear #clear screen tput sc #save current cursor position tput cup 10 13 #move cursor to row col tput civis #cursor invisible tput cnorm #cursor visible tput rc #display output exit 0 move cursor
Using tput makes it easy to move the cursor position across devices. By using the cup option in tput, or cursor position, you can move the cursor to any X or Y coordinate in the rows and columns of the device. The coordinates of the upper-left corner of the device are (0,0).
To move the cursor to row 1 (Y) of column 5 (X) on a device, simply execute tput cup 5 1. Another example is tput cup 23 45, which moves the cursor to line 45 on column 23.
Move the cursor and display information
Another useful cursor positioning technique is to move the cursor, execute a command to display information, and then return to the previous cursor position:
(tput sc ; tput cup 23 45 ; echo "Input from tput/echo at 23/45" ; tput rc) Let's analyze the subshell command:
tsc put must first save the current cursor position. To save the current cursor position, include the sc option or "save cursor position."
tput cup 23 45 After saving the cursor position, the cursor coordinates move to (23,45).
echo "Input from tput/echo at 23/45" Displays information to stdout.
tput rc After displaying this information, the cursor must return to the original position saved using tput sc. To return the cursor to its last saved position, include the rc option or "restore cursor position."
Note: since this article first details executing tput from the command line, you might find it simpler to execute commands in your own subshell than to execute each command individually and then display a prompt before each command is executed.
Change the properties of the cursor
There are times when you don't want to see a cursor when displaying data to a device. Turning the cursor invisible can make the screen look cleaner when data scrolls. To make the cursor invisible, use the civic option (for example, tput civic). After the data is fully displayed, you can use the cnorm option to turn the cursor visible again.
text properties
Changing the way text is displayed can draw attention to a group of words in a menu or alert users to something important. You can change text properties by bolding text, underlining text, changing background and foreground colors, and reversing color schemes.
To change the color of text, use the setb option (for setting the background color) and the setf option (for setting the foreground color) and the color values assigned in the terminfo database. In general, assigned values correspond to colors as follows, but this may vary from UNIX system to UNIX system:
0: Black
1: Blue
2: Green
3: Cyan
4: Red
5: Magenta
6: Yellow
7: White
Execute the following example command to change the background color to yellow and the foreground color to red:
tput setb 6 tput setf 4 To reverse the current color scheme, simply execute tput rev.
Sometimes coloring the text isn't enough, meaning you want to get the user's attention in another way. This can be done in two ways: by making the text bold or by underlining it.
To change text to bold, use the bold option. To start underlining, use the smul option. After finishing displaying underlined text, use the rmul option.
Examples make the output string colored, undercolored, bold:
#!/ bin/bash printf $(tput setaf 2; tput bold)'color show\n\n'$(tput sgr0) for((i=0; i=7; i++)); do echo $(tput setaf $i)"show me the money"$(tput sgr0) done printf '\n'$(tput setaf 2; tput setab 0; tput bold)'background color show'$(tput sgr0)'\n\n' for((i=0,j=7; i=7; i++,j--)); do echo $(tput setaf $i; tput setab $j; tput bold)"show me the money"$(tput sgr0) done exit 0 Output format control function:
#!/ bin/bash # $1 str print string # $2 color 0-7 Set Color # $3 bgcolor 0-7 Set Background Color # $4 bold 0-1 Set bold # $5 underline 0-1 Set underscore function format_output(){ str=$1 color=$2 bgcolor=$3 bold=$4 underline=$5 normal=$(tput sgr0) case "$color" in 0|1|2|3|4|5|6|7) setcolor=$(tput setaf $color;) ;; *) setcolor="" ;; esac case "$bgcolor" in 0|1|2|3|4|5|6|7) setbgcolor=$(tput setab $bgcolor;) ;; *) setbgcolor="" ;; esac if [ "$bold" = "1" ]; then setbold=$(tput bold;) else setbold="" fi if [ "$underline" = "1" ]; then setunderline=$(tput smul;) else setunderline="" fi printf "$setcolor$setbgcolor$setbold$setunderline$str$normal\n" } format_output "Yesterday Once more" 2 5 1 exit 0 Cursor properties Example:
#!/ bin/bash # clear the screen tput clear # Move cursor to screen location X,Y (top left is 0,0) tput cup 3 15 # set a foreground colour using ANSI escape tput setaf 3 echo "XYX Corp LTD. " tput sgr0 tput cup 5 17 # Set reverse video mode tput rev echo "M A I N - M E N U" tput sgr0 tput cup 7 15 echo "1\. User Management" tput cup 8 15 echo "2\. service Management" tput cup 9 15 echo "3\. Process Management" tput cup 10 15 echo "4\. Backup" # Set bold mode tput bold tput cup 12 15 read -p "Enter your choice [1-4] " choice tput clear tput sgr0 tput rc exit 0 above is" How to use tput command in Linux "All the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.