Shell script: test command if-then for while learning notes

Source: Internet
Author: User

Test File OPERATOR:

-B file: if the file is a special file, it is true.

-C file: if the file is a special character file, it is true.

-D file: if the file is a directory, it is true.

-E file: true if the file exists

-F file: if the file is a normal file, it is true.

-G file: If the SGID bit of the file is set, it is true.

-G file: true if the file exists and belongs to the group

-K file: true if the adhesion bit of the file is set

-O file: true if the file exists and belongs to the user

-P file: if the file is a named pipe, it is true.

-R file: true if the file is readable

-S file: true if the file length is not zero

-S file: if the file is a special socket file, it is true.

-T fd: If fd is an open file descriptor connected to a terminal (fd is 1 by default), it is true.

-U file: If the SUID bit of the file is set, it is true.

-W file: true if the file is writable

-X file: true if the file is executable


If-then structured command instance:

file=test1touch fileif [ -s file ]then        echo " The file file exits and has data in it"else        echo " The file file exits ,but  has not  data in it"fidate > fileif [ -s file ]then        echo "the file file has data in it"else        echo "the file file is also empty ,no data hummm"fiif [ 1.c -nt one2.c ]then        echo "1.c is newer than one2.c"else        echo "1.c is not newer than one2.c"fi


For command:

For var in list
Do
Commands
Done

Instance:

List = "one one2 one4 one5" list = $ list "one6" // Add new data to an existing variable for state in $ listdo echo "state is $ state" done result :. /forteststate is onestate is one2state is one4state is one5state is one6

Read the value from the command: file = "states" for state in 'cat $ file' do echo "visit word $ state" done ~

While command

Format:

While the command
Do
Other commands
Done


Instance:

Var1 = 10 # assign a value if there is no space or a space. For example,/last: 3rd rows: [:-gt: expect the echo "var1 is $ var1" while [$ var1-gt 0] do echo "the var1 is $ var1" var1 = $ [$ var1-1] done

Result:

the var1 is 10the var1 is 9the var1 is 8the var1 is 7the var1 is 6the var1 is 5the var1 is 4the var1 is 3the var1 is 2the var1 is 1


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.