Exercise content:
When the.txt file exists under the folder, if the.txt file exists, a new a-1.txtfile is created. If the file does not exist, the t.txt file is created;
Three conditional expressions of if
1,
If condition 1; then
Command1
Else
Command2
Fi
Code:
1 #! /Bin/bash
2
3 if ls | grep a.txt; then
4 touch a-1.txt
5 else
6 touch t.txt
7 fi
LOONG:/home/Yee/if # ls
If-1.sh if-2.sh
LOONG:/home/Yee/'if # sh-x if-1.sh
+ Ls
+ Grep a.txt
+ Touch t.txt
LOONG:/home/Yee/if # ls
If-1.sh if-2.sh t.txt
LOONG:/home/Yee/if # Touch a.txt
LOONG:/home/Yee/if # ls
A.txt if-1.sh t.txt
LOONG:/home/Yee/'if # sh-x if-1.sh
+ Ls
+ Grep a.txt
A.txt
+ Touch a-1.txt
LOONG:/home/Yee/if #
2,
If [expression]; leave a space between then if and []; otherwise, an error is returned;
Command1
FI semicolon; no fewer; otherwise, the following error occurs: syntax error: unexpected end of file; if the semicolon is not used, then should be written in a new line;
In more complex cases, you can use this syntax:
If [condition 1]; then
When the conditional statement is created, instructions can be executed;
Elif [Condition Two]; then
Command work content that can be executed when the condition is set to two;
Else
Instructions that can be executed when neither condition 1 nor condition 2 is met;
Fi
File expression
Conditional expressions
If [-FFile] If the file exists
If [-d... ] If the directory exists
If [-s file] If the file exists and is not empty
If [-r file] If the file exists and is readable
If [-W File ] If the file exists and can be written
If [-X file ] If the file exists and is executable
If [-e dir | file] returns true if the specified file or directory exists.
[-Z string] If the string length is zero, it is true.
[-N string] true if the string length is not zero
[String1 = string2] true if the two strings are the same
[String1! = String2] true if the strings are different
Integer variable expression
If [int1-EQ int2] If int1 is equal to int2
-EQ -Ne -Lt
-NT can only be used as an integer and is not applicable to strings. A string equals to a value =
If [int1-ne int2] If not equal
If [int1-ge int2] If the> = integer conditional expression is greater than, less than, and shell does not have> and <, it will be treated as angle brackets, only-Ge,-GT,-Le, lt
If [int1-GT int2] If>
If [int1-Le int2] If <=
If [int1-lt int2] If <
String variable expression
If[$ A = $ B]
If string1 is equal to string2The variable referenced by the conditional expression must contain $, compare $ A and $ B, instead of comparing a and B.
A string can be equal by a value.
If[$ String1! =$ String2] If string1 is not equal to string2 = Assign values in other places and place values in if
[] Indicates that when the string is equal to or = is equal to, spaces must be added to both sides of the string; otherwise, the string is invalid.
If[-N $ string] If the string is not null (not 0), 0 (true) is returned)
If[-Z $ string] If the string
Null
If[$ Sting] If the string is not null, 0 is returned (similar to-N)
Non-logical! Inverse of conditional expressions
If [! Expression]
If [! -D $ num] If the directory does not exist $ num
Logic and- Combination of conditional expressions
If [expression 1-Expression 2]
Logic or-o Conditional expression or
If [expression 1-O expression 2]
Logical expression
- Expression and the =! =-D-F-X-ne-eq-lt
- Logical symbols are normally connected to other expressions without any parentheses ().
If [-z "$ jhhome"-a-d $ home/
Note that logic and-A and logic or-O are easily mixed with the operator numbers of other strings or files.
Code:
1 #! /Bin/bash
2
3 filename =/home/Yee/If/a.txt
4 if [-F $ filename]; then
5 touch a-1.txt
6 fi
7 if [! -E $ filename]; then
8 touch t.txt
9 fi
LOONG:/home/Yee/if # ll
Total 12
-RW-r -- 1 Root 75 11-07 :42 if-1.sh
-RW-r -- 1 Root 130 11-07 if-2.sh
-RW-r -- 1 Root 38 11-07 14:21 if-3.sh
LOONG:/home/Yee/'if # sh-x if-2.sh
+ Filename =/home/Yee/If/a.txt
+ '['-F/home/Yee/If/a.txt ']'
+ '[''! '-E/home/Yee/If/a.txt']'
+ Touch t.txt
LOONG:/home/Yee/if # ll
Total 12
-RW-r -- 1 Root 75 11-07 :42 if-1.sh
-RW-r -- 1 Root 130 11-07 if-2.sh
-RW-r -- 1 Root 38 11-07 14:21 if-3.sh
-RW-r -- 1 Root 0 11-07 16:11 t.txt
LOONG:/home/Yee/if #
3,
If test expression; then
Command1
Fi
Example:
If test $ num-eq0 Equivalent If [$ num-EQ 0]
Test Expression, no [ ]
1 #!/bin/bash 2 3 filename=/home/yee/if/a.txt 4 if test -f $filename 5 then 6 touch a-1.txt 7 else 8 touch t.txt 9 fi
LOONG: /home/Yee/if # ll total 12-rw-r -- r -- 1 Root 75 11-07 if-1.sh-rw-r -- r -- 1 Root 130 11-07 if-2.sh-rw-r -- r -- 1 Root 38 11-07 if-3.sh-rw-r -- r -- 1 Root 0 11-07 t.txt LOONG: /home/Yee/if # sh-x if-3.sh + filename =/home/Yee/If/a.txt + test-f/home/Yee/If/a.txt + touch t.txt LOONG: /home/Yee/if # ll total 12-rw-r -- r -- 1 Root 75 11-07 if-1.sh-rw-r -- r -- 1 Root 130 11-07 if-2.sh-rw-r -- r -- 1 Root 103 11-07 if-3.sh-rw-r -- r -- 1 Root 0 11-07 t.txt LOONG: /home/Yee/if # Touch a.txt LOONG: /home/Yee/if # sh-x if-3.sh + filename =/home/Yee/If/a.txt + test-f/home/Yee/If/a.txt + touch a-1.txtLoong: /home/Yee/if # ll total 12-rw-r -- r -- 1 Root 0 11-07 a-1.txt-rw-r -- r -- 1 Root 0 11-07 a.txt-RW-r -- r -- 1 Root 75 11-07 if-1.sh-rw-r -- r -- 1 Root 130 11-07 if-2.sh-rw-r -- r -- 1 Root 103 11-07 if-3.sh-rw-r -- r -- 1 Root 0 11-07 16:20 t.txt LOONG: /home/Yee/if #
The echo-e parameter makes the description of the backslash (\) in the output take effect.
The echo-N parameter causes the quoted content to be output (without line feed)