Shell learning: Shell echo, printf, test command, echotest

Source: Internet
Author: User
Tags echo command

Shell learning: Shell echo, printf, test command, echotest
Shell learning: Shell echo, printf, test command Shell echo command

Show normal strings

echo "hello"

Output: hello

Double quotation marks can be omitted as follows:

echo hello

Output: hello

Show escape characters

Echo "\" hello \ "" or echo \ "hello \"

Output: "hello"

Show Variables

name="tom"echo "$name is a child"

Output: tom is a child

Display line feed

#-E: Enable escape echo-e "hello \ nworld"

Output:

helloworld

The display result is directed to the file.

echo "hello world" > myfile.txt

Run cat myfile.txt and Output

hello world

Display date

echo `date`

Output: Tue Jan 16 06:30:53 PST 2018.

Shell printf command

The Shell printf command imitates the printf () program in the C library.

Printf is defined by the POSIX standard. Therefore, it is better to use the printf script than echo for portability.

Syntax of printf:

printf format-string [arguments...]
Format-string: format control string arguments: list of parameters

Eg:

[Zhang @ localhost ~] $ Echo "hello, shell" hello, shell # if there is no line break, do not wrap [zhang @ localhost ~] $ Printf "hello world" hello world [zhang @ localhost ~] $ # Use the line break [zhang @ localhost ~] $ Printf "hello, shell \ n" hello, shell

Printf placeholder Function

Eg:

#!/bin/bashprintf "%-10s %-8s %-4s\n" name sex weightprintf "%-10s %-8s %-4.2f\n" zhangsan male 68.22printf "%-10s %-8s %-4.2f\n" lisi male 70.02printf "%-10s %-8s %-.2f\n" xiaolongnv female 48.12

Output:

name       sex      weightzhangsan   male     68.22lisi       male     70.02xiaolongnv female   48.12

Placeholders include % s, % d, % c, and % f.

%-10 s: the width is 10 characters.-indicates the left alignment, but not the right alignment. If not, spaces are automatically filled. Otherwise, all content is displayed.

%-4.2f: Indicates formatting decimal places, where. 2 indicates retaining two decimal places.

% D: format a number

Shell test command

The test command in Shell is used to check whether a condition is true. It can be used to test values, characters, and files.

Numerical Test

Parameters Description
-Eq Equal to true
-Ne True if not equal
-Gt True if the value is greater
-Ge True if the value is greater than or equal
-Lt True if the value is smaller
-Le True if the value is less than or equal

Eg:

#!/bin/bashnum1=100num2=200if test $num1 -eq $num2then    echo "$num1 equal $num2"else    echo "$num1 not equal $num2"fi

Result:

100 not equal 200

String Testing

Parameters Description
= Equal to true
! = True if not equal
-Z If the string length is 0, it is true.
-N If the string length is not 0, it is true.

Eg:

#!/bin/bashstr1="hello"str2="world"if test $str1 = $str2then    echo "$str1 equal $str2"else    echo "$str1 not equal $str2"fiif test -z $str1then    echo "$str1 is empty"else    echo "$str1 is not empty"fi

Output result:

hello not equal worldhello is not empty

File Test

Parameters Description
-E True if the file exists
-R True if the object exists and is readable
-W True if the file exists and can be written
-X True if the file exists and is executable
-S True if the file exists with at least one character
-D True if the file exists and is a directory
-F True if the object exists and is a file
-C True if the object exists and is a special string File
-B True if the object exists and is a special object

Eg:

Run the ls-l command in the user directory of zhang:

Run test-e Command:

[zhang@localhost ~]$ test -e 1.sh[zhang@localhost ~]$ if test -e 1.sh> then>     echo "1.sh file exist"> else>     echo "1.sh file not exist"> fi1.sh file exist

Run test-r Command:

[zhang@localhost ~]$ if test -r 1.sh> then>     echo "1.sh read ok"> else>     echo "1.sh read not ok"> fi1.sh read ok

Run test-x Command:

[zhang@localhost ~]$ if test -x myfile.txt> then >     echo "myfile.txt is execute file"> else>     echo "myfile.txt is not execute file"> fimyfile.txt is not execute file

Run test-d Command:

[zhang@localhost ~]$ if test -d test> then>     echo "test is a dir"> else>     echo "test is not a dir"> fitest is a dir
<

Related Article

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.