Shell conditional expression

Source: Internet
Author: User

1. The common syntax for conditional testing is as follows

1, test expression

2. [Test expression] #两边需要有空格

3, [[Test expression]]

4, (test expression))

Description

The first and second are equivalent, the third is the extended Test command, and syntax 4 is often used to calculate

You can use wildcard characters in [[]] double brackets to make pattern matching,&& | | > < operators can be applied directly in double brackets, but not in single brackets

2, test A simple example of the following, you can view the man test for detailed help

[[email protected] ~]# test-f/etc/passwd && echo 1 | | Echo 0
1
[[email protected] ~]# test-f/etc/passwd11 && echo 1 | | Echo 0
0

3, [] but the bracket instance

[[Email protected] ~]# [-f/etc/passwd]&& echo 1 | | Echo 0
1
[[Email protected] ~]# [-f/etc/passwd11]&& echo 1 | | Echo 0
0

4, [[]] double bracket instance

[[Email protected] ~]# [[3 > 2]]&& echo 1 | | Echo 0
1
[[Email protected] ~]# [[3 > 4]]&& echo 1 | | Echo 0
0

5. File Test expression

[[Email protected] ~]# [-d/etc/]&& echo 1 | | Echo 0
1
[Email protected] ~]#
[[Email protected] ~]# [-D/ETC/11]&& echo 1 | | Echo 0
0

6. String Test expression

[[Email protected] ~]# [-N "abc"]&& echo 1 | | Echo 0
1

[[Email protected] ~]# [-z] "]&& echo 1 | | Echo 0
0
[[Email protected] ~]# ["AB" = "AB"]&& echo 1 | | Echo 0
1
[[Email protected] ~]# ["AB"! = "AB"]&& echo 1 | | Echo 0
0

Observing how the system scripts are written

7. Integer comparison

[[Email protected] ~]# [2-lt 3]&&echo 1| | Echo 0
1
[[Email Protected]ckup ~]# [2-gt 3]&&echo 1| | Echo 0
0

Compare the size of two integers [[email protected]~]#Cat 1.SH #!/bin/bash#no.1a=$1b=$2[ $# -ne2]&&{  Echo "USAGE:"$0 "NUM1 num2"Exit1} #no.2Expr$a + ten &>/dev/dullRETVAL1=$?Expr$b + Ten &>/dev/dullRETVAL2=$?[$RETVAL 1-eq0-A $RETVAL 2-eq0]||{  Echo "pls-input-int nums:"exit 2} #no.3[$a-GT $b] &&Echo "$a > $b"||{[$a-eq $b] &&Echo "$a = $b"||Echo "$a < $b"}[[email protected]~]#SH 1.SH 1 21<2[[Email protected]~]#SH 1.SH 3 23>2[[Email protected]~]#SH 1.SH 2 22=2


Use read mode instead

[email protected] ~]# cat 1.sh
#!/bin/bash
#no. 1
Read-p "Pls input" num: "a B
[-Z "$a"]| | [-Z "$b"]&&{
echo "pls input" num again "
Exit 1
}
#no. 2
Expr $a + &>/dev/dull
Retval1=$?
Expr $b + &>/dev/dull
Retval2=$?
Test $RETVAL 1-eq 0-a $RETVAL 2-eq 0 | | {
echo "pls input" int nums: "
Exit 2
}

#no. 3
[$a-gt $b] && echo "$a > $b"
[$a-lt $b] && echo "$a < $b"
[$a-eq $b] && echo "$a = $b"

[[Email protected] ~]# sh 1.sh
Pls Input 1 Num:1
1=1
[[Email protected] ~]# sh 1.sh
Pls Input 2 Num:1
1<2
[[Email protected] ~]# sh 1.sh
Pls Input 1 Num:2
2>1

Shell conditional expression

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.