Linux Shell control statements

Source: Internet
Author: User

Control statement

The following three methods are commonly used. A lower-case string represents a control command, while a lower-case string represents other meanings.
1. If test then commands Elif test then commands fi
2. for VAR in list do commands done
3. While test do commands done

Commands refers to the command set; var refers to the variable; test is a test statement, there are several forms:

View code

 1   [  -  A file] If the file exists, it is true.
2 [ - B file] If the file exists and is a special block file, it is true.
3 [ - C file] If the file exists and is a special word file, it is true.
4 [ - D file] If the file exists and is a directory, it is true.
5 [ - E file] If the file exists, it is true.
6 [ - F file] If the file exists and is a normal file, it is true.
7 [ - G file] If the file exists and the sgid has been set, it is true.
8 [ - H file] If the file exists and is a symbolic connection, it is true.
9 [ - K file] It is true if the file exists and the stick bits have been set.
10 [ - P file] If the file exists and is a name pipe (f If O), it is true.
11 [ - R file] true if the file exists and is readable.
12 [ - S file] If the file exists and the size is not 0, it is true.
13 [ - T FD] It is true if the file descriptor FD is open and points to a terminal.
14 [ - U file] If the file exists and SUID ( Set User ID) is true.
15 [ - W file] If the file exists and is writable, it is true.
16 [ - X file] true if the file exists and is executable.
17 [ - O file] If the file exists and is a valid user ID, it is true.
18 [ - G file] If the file exists and belongs to a valid user group, it is true.
19 [ - L file] If the file exists and is a symbolic connection, it is true.
20 [ - N file] If file exists and has been mod, it is true if IED since it was last read.
21 [ - S file] If the file exists and is a socket, it is true.
22 [File1 - NT file2] If file1 has been changed more recently than file2, or if file1file2 does not is true. Exists and
23 [File1 - Ot file2] True if file1 is older than file2, or file2 exists and file1 does not exist.
24 [File1 - EF file2] True if file1 and file2 point to the same device and node number.
25 [ - O optionname] true if the shell option "optionname" is enabled.
26 [ - Z string] If the length of string is zero, it is true.
27 [ - The length of N string] or [String] string is non-zero. - Zero is true.
28 [String1 = String2] If the two strings are the same. = May be used instead = For Strict POSIX compliance is true.
29 [String1 ! = String2] true if the strings are not equal.
30 [String1 < String2] If string1 sorts before string2 lexicographically In The current locale is true.
31 [String1 > String2] If string1 sorts after string2 lexicographically In The current locale is true.
32 [Arg1 op arg2] OP Is One - EQ, - Ne, - LT, - Le, - GT or - Ge.
33
34 [ ! Expr] True if expr is false.
35 [(Expr)] returns the value of expr. This can be used to ignore normal operator priorities.
36 [Expr1 - A expr2] True if expr1 and expr2 are all true.
37 [Expr1 - O expr2] True if expr1 or expr2 is true.
38
39 The test command can also be returned. For example
40 If " Hello World " | Grep hello > / Dev / Null ; Then ECHO " Hello is here " ; FI

4. Example of if:

 
If [$ count-lt $ times] Then count = 'expr $ count + 1' echo $ count fi

5. For example:

 
For Loop in 'LS' do echo $ loop done

6. Example of while:

 
While [$ count-lt $ times] do count = 'expr $ count + 1' echo $ count done

Notes
1. Be careful with some spaces. For example, in if ["$ Str" = "ABC"], spaces before and after [and] cannot be removed.
2. Expression of the variable. By default, shell is executed by string. Therefore:
A = 1
A = $ A + 1
Then a becomes the string "1 + 1", not 2. If he runs in mathematics, use the expr command.
A = 1
A = 'expr $ A + 1'
Note that the leading and trailing spaces cannot be removed here. Otherwise, "$ A +" will be seen as a whole, and all parsing errors will occur.

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.