In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Guide when writing a shell script, we use echo to print to standard output. Echo is a simple command, but its functionality is limited. For better control over the output format, you can use the printf command.
The syntax format of printf:
# printf format parameters
Here is an example:
[root@localhost ~] # printf "name:% s\ nheight:% dcm\ nweight:% dkg\ n" Xiaoming "180," 75" name: Xiaoming height: 180cm weight: 75kg
"name:% s\ nheight:% dcm\ nweight:% dkg\ n" is the format, "Xiaoming"180"75" is the parameter. The format contains% s,% d is a format conversion character, and the parameter corresponding to% d must be a decimal number. The parameter for s must be a string. Three newline characters are also included. The printf command does not add line return symbols by default, but needs to be added manually.
Commonly used escape character\ "- escaped double quotes\-escaped backslash\ b-backspace character\ n-newline character\ r-carriage return\ t-horizontal tab character\ v-Vertical tab%-Type conversion character commonly used in a single% symbol
The type conversion specifier is a character that specifies how to interpret the corresponding parameter, which must be added. The following is a list of all types of conversions and their functions:
% d-print parameter as decimal integer% f-print parameter as floating point number% s-print parameter as string% x-print parameter as hexadecimal integer% o-print parameter as octal integer instance 1
This example uses% dforce% xmy% o to convert the parameters provided later into decimal, hexadecimal, and octal.
[root@localhost ~] # printf "Decimal:% d\ nHex:% x\ nOctal:% o\ n" 100100 100Decimal: 100Hex: 64Octal: 144Instanc II
The following example uses% .2f, where .2 means that the parameter retains two decimal places and f prints the parameter as a floating point number.
[root@localhost ~] # printf "% .2f\ n" 3.14159263.14 instance 3
The horizontal tab character\ t is used in the following example. The parameters given in the example exceed the number of conversion characters given in the format, and the extra parameters will still be output according to the format.
[root@localhost] # printf "% s\ t% s\ t% s\ n"name", "gender", "age", "Xiaoming", "male", "Xiao Hong", "female", "19", "Little Blue", "male", "18" name, gender age, 18 Xiaoming male, 19 Xiaolong female, 18 Xiaolan male.
Example 4
In the instance,%-10s,%-8d, where-indicates left alignment, and right alignment by default. 10 and 8 represent a width of 10 or 8 characters. \ e [1BO 30th 47m. \ e [0m means to use bold type, black font, white background, followed by\ e [0Magnem] to indicate reset.
[root@localhost ~] # cat p.sh #! / bin/bashprintf "\ e [1Trading 30th 47m% Mutual 10s%-10s\ e [0m\ n", "name", "age" printf "\ e [36Trans47m% Musical 10s%-8d\ e [0m\ n"Xiaoming"18" printf "\ e [31X 47m% Mutual 10s%-8d\ e [0m\ n"Xiao Hong"19" printf "\ e [34] 47m%-10s%-8d\ e [0m\ n "" Little Blue "" 19 "
Summary
When writing shell scripts, we use echo to print to standard output. Echo is a simple command, but its functionality is limited. For better control over the output format, you can use the printf command.
Original address of this article: https://www.linuxprobe.com/linux-printf-example.html
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.