Shell script structure, date command, variable

Source: Internet
Author: User

20.5 logical judgments in shell scripts

Logical Expressions

In the [] bracket:

-lt:=little than less than
-le:=little && equal less than equals
-eq:=equal equals
-ne:=no Equal Not equal to
-gt:=greater than greater than
-ge:=greater && equal greater than or equal
In (()) parentheses:
<,<=,==,!=,>,>=

Note: Use double brackets to enclose

Format 1

if condition; then Commond;fi

eg

#!/bin/bash
A=5
If [$a-GT 3]
Spaces within the #注意 []
Then
echo "OK"
Fi
Format 2

If condition 1;then commond1;else commond2;fi

eg

#!/bin/bash
A=5
If [$a-GT 3]
Spaces within the #注意 []
Then
echo "OK"
Else
echo "Fault"
Fi
Format 3

If condition 1;then commond1;elif condition 2;then commond2;else commond3;fi

eg

#!/bin/bash
A=5
If [$a-lt 3]
Spaces within the #注意 []
Then
echo "A<3"
elif [$a-GT 6]
Then
echo "A>6"
Else
echo "Out of the zone"
Fi
Relationship

The relationships between the various conditions can use logical connectors:

Condition a&& condition B: and
Condition a| | Condition B: or

20.6 File Directory property judgment

The IF in a shell script is often used to determine the properties of a document, such as whether it is a normal file or a directory file, to determine if the file has read, write, execute permissions, and so on. The If common options are the following:

-E: Determine if a file or directory exists
-D: Determine if the directory file is not present
-F: Determine if the file is not normal and whether it exists
-r: Determine if Read permission is available
-W: Determine if Write permission is available
-X: Determine if EXECUTE permission is available
Format

If a file exists:

If [-e filename]
Then
Commond
Fi
The above commands can be simplified to:

[-e filename] && commond
&& before and after the life is executed

Or:
[-e FileName] | | Commond
When | | The previous execution succeeds before executing the subsequent command
If a file does not exist:

if [!-e FileName]
Then
Commond
Fi
“! "is equivalent to taking the reverse.

20.7 If special usage

If [-Z ' $a]: Indicates what happens when the value of variable A is empty
If [-n ' $a]: Indicates what happens when the value of variable A is not empty
-Z and-N are the opposite two counter-conditions.

eg

#!/bin/bash
n=wc -l /tmp/test.txt
If [$n-gt 20]
Then
Echo 1
Else
Echo 0
Fi
There is no syntax error in this script, only we preset/tmp/test.txt is present, if the file does not exist, the script will be executed with an error:

[Email protected] sbin]# sh if.sh
WC:/tmp/test.txt: No file or directory
if.sh: Line 3rd: [:-gt: Expecting unary expression
Therefore, in order to avoid this error, you need to write a more rigorous script, you need to do "if [$n-gt 20]" before the implementation of the file "/tmp/test.txt" to confirm the existence of:

#!/bin/bash
n=wc -l /tmp/test.txt
If [-Z $n]
Then
echo "Error"
Exit
elif [$n-lt 20]
Then
Echo 1
Else
Echo 0
Fi
That is, if the variable n is empty, the error is displayed and the script exits.

[Email protected] sbin]# sh if.sh
WC:/tmp/test.txt: No file or directory
Error
That is, when the file does not exist, it exits execution without prompting for a syntax error. (There is a logic error in the script, only the effect is shown)

Note: Use double quotation marks when referencing variables in this expression.

If grep ' 123 ' Test.txt;then: Indicates what happens when the test.txt contains 123
eg

To determine the existence of a parameter:
[Email protected] sbin]# vim if1.sh
#!/bin/bash
If
Grep-wq ' user1 '/etc/passwd
Then
echo "User1 exist."
Fi
[Email protected] sbin]# sh if1.sh

To determine that a parameter does not exist:
[Email protected] sbin]# vim if2.sh
#!/bin/bash
If
! Grep-wq ' user1 '/etc/passwd
Then
echo "No User1"
Fi
[Email protected] sbin]# sh if2.sh
No User1
Description: The-w option =word in grep, which means filtering a word;-Q, which means that the results of filtering are not printed. Use when judging that a parameter does not exist! Represents the inverse.

20.8-20.9 Case Judgment

Format:

Case variable name in
value1)
Commond1
;;
value2)
Commod2
;;
VALUE3)
Commod3
;;
Esac
In a case, you can use a "|" in a condition to denote or mean, such as:

2|3)
Commond
;;
eg

[Email protected] sbin]# vim case1.sh
#!/bin/bash
Read-p "Please input a number:" N
If [-Z "$n"]
Then
echo "Please input a number."
Exit 1
# "Exit 1" indicates the return value after executing the partial command
#即, use the value of ECHO $ after the command has finished executing
Fi

n1=echo $n|sed ‘s/[0-9]//g‘
#判断用户输入的字符是否为纯数字
#如果是数字, it is replaced with a null, assigned to $N1
If [-N "$n 1"]
Then
echo "Please input a number."
Exit 1
#判断 $n 1 is not empty (that is, $n is not a pure number) again prompts the user to enter a number and exit
Fi

If the user enters a pure number, execute the following command:
If [$n-lt] && [$n-ge 0]
Then
Tag=1
elif [$n-ge] && [$n-LT 80]
Then
tag=2
elif [$n-ge] && [$n-LT 90]
Then
Tag=3
elif [$n-ge] && [$n-le 100]
Then
Tag=4
Else
Tag=0
Fi
#tag的作用是为判断条件设定标签, conveniently referenced later
Case $tag in
1)
echo "Not OK"
;;
2)
echo "OK"
;;
3)
echo "Ook"
;;
4)
echo "Oook"
;;
*)
echo "The number range is 0-100."
;;
Esac
Description: The purpose of this script is to enter test scores and determine the grade of test scores.

Execution Result:

[Email protected] sbin]# sh case1.sh
Please input a number:90
Oook
[Email protected] sbin]# sh case1.sh
Please input a number:80
Ook
[Email protected] sbin]# sh case1.sh
Please input a number:60
Ok
[Email protected] sbin]# sh case1.sh
Please input a number:55
Not OK

Shell script structure, date command, variable

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.