Linux shell print output, linuxshell

Source: Internet
Author: User

Linux shell print output, linuxshell
Introduction

I often need to deal with shell commands, but I have never studied the system. Next I will spend one to two months studying shell commands, next, let's start a wonderful trip with shell commands. This chapter mainly introduces the print output of shell.

 

 

Knowledge points

  • Shell scripts all use #! Start with/bin/bash. This is a fixed method, where/bin/bash is the path of the bash command.
  • Generally, the shell script executable permission is granted through chmod.
  • The Print Output in shell scripts usually contains two types: echo and printf. The former will automatically wrap.
  • In shell, if double quotation marks ("") are used as the output content, if the double quotation marks contain special characters, you must add the Escape Character \ before the special characters \, of course, you can also use single quotes or directly output without quotation marks. If you use single quotes or do not use them, you do not need to use escape characters.

 

Format substitution character

Some common format substitution characters are often used in the print output, and the format substitution characters can only be used in the printf output.

%-5 s: indicates the width of five characters, where-indicates the left alignment

%-4.2f: f indicates the floating point type. 4.2 indicates that the length is 4 characters and the decimal point is 2 digits. If the decimal point is greater than 2, it is rounded to the nearest digit.-indicates the left alignment.

Integer:

% D: the integer parameter is converted into a signed decimal number.

% U: the integer parameter is converted into an unsigned decimal number.

% O: the integer is converted into an unsigned octal number.

% X: the integer parameter is converted into an unsigned hexadecimal number and expressed in lowercase abcdef.

% X: the integer is converted into an unsigned hexadecimal number and expressed in ABCDEF.


Float count:

% F double type parameters are converted to decimal numbers. The default value is six digits below the decimal point.

Parameters of the % e double type are printed in exponential form. A number is placed before the decimal point, and the six digits are placed after the decimal point, while the exponent part is expressed in lowercase e.

% E has the same effect as % e. The only difference is that the exponent part is expressed by an uppercase E.

Parameters of the % g double type are automatically printed in the format of % f or % e. The standard is determined based on the value to be printed and the specified number of valid digits.

The difference between % G and % g is the same. The only difference is that % eformat will be selected for exponential printing.


Character and string:

% C read the first character of the string

% S output character content of the specified width.

% P if the parameter is a "void *" pointer, it is displayed in hexadecimal format.

 

Print Output

When writing a script, echo is often used as the print output, and double quotation marks are often used.

Echo

[root@localhost tmp]# echo "hello word"hello word

Printf

By default, printf does not provide line breaks.

[root@localhost tmp]# printf "hello word"hello word[root@localhost tmp]# 

The \ n parameter must be added to the newline.

[root@localhost tmp]# printf "hello word\n"hello word[root@localhost tmp]# 

Write shell scripts

Vim scrip. sh

#!/bin/bashprintf "%-5s %-10s %-4s\n" No Name Mark;printf "%-5s %-10s %-4.2f\n" 1 aaa 10.111;printf "%-5s %-10s %-4.2f\n" 2 bbb 20.146;

Grant the script execution permission: chmod u + x scrip. sh

 

If echo is used, the format replacement character cannot be used.

#!/bin/bashecho No Name Mark;echo 1 aaa 10.111;echo 2 bbb 20.146;

 

Notes

When the-e and-n parameters are used in echo and printf,-e and-n should appear before other characters in the command line.

-E: If you want to use the escape sequence as a parameter in the double quotes of echo, you must use the-e parameter.

-N: Ignore the line break at the end

Do not use the-e parameter [root @ localhost tmp] # echo "1 \ n2" 1 \ n2
Use the-e parameter [root @ localhost tmp] # echo-e "1 \ n2" 12

Escape sequence:

/N: line feed

/T: tab key

 

Color output

Font color: 0 = reset, 30 = Black, 31 = red, 32 = green, 33 = yellow, 34 = blue, 35 = Magenta, 36 = cyan, 37 = white

Background colors include: 0 = reset, 40 = Black, 41 = red, 42 = green, 43 = yellow, 44 = blue, 45 = Magenta, 46 = cyan, 47 = white

echo -e "\e[1;32m hello word \e[0m"

\ E [1; 32 m: Set the font color to green, \ e [0 m: reset the color

Summary

Shell commands are very powerful, and we will continue to post articles on shell learning later.

 

 

Note:

Author: pursuer. chen

Blog: http://www.cnblogs.com/chenmh

All essays on this site are original. You are welcome to repost them. However, you must indicate the source of the article and clearly give the link at the beginning of the article.

Welcome to discussion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.