If logic judgment, file directory attribute judgment, if special usage, case judgment in 20.5-20.8 shell script

Keywords: shell Attribute less vim

if Logical Judgment in 20.5 shell Script

Shell script is full of all kinds of logical judgments, which is necessary in the script.

Logical Judgment Expressions:

- GT (>); greater than great than
 -lt(<); less than less than
 - Ge (>=); greater than or equal to   
- Le (<=); less than or equal to
 - eq(===); equal
 - ne(!=) is not equal to not equa
- - -

for example
if [ $a -gt $b ]; 
if [ $a -lt 5 ]; 
If [$b-eq 10], etc.

if logic judgment format

Format 1: if condition; then statement; fi
 Format 2: if condition; then statement; else statement; fi
 Format 3: if... ; then... ; elif... ; then... ; else... ; fi
- - -

You can use & & & | to combine multiple conditions
 Conditions A & Conditions B: A and B
 Conditions A | | Conditions B: A or B
if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [ $b -lt 3 ]; then

if logic judgment example

Format 1: if condition; then statement; fi

#!/bin/bash
a=5
if [ $a -gt 3 ]
#Note that there are a lot of spaces in []
then
       echo "ok"
fi

Format 2: if condition; then statement; else statement; fi

#!/bin/bash
a=5
if [ $a -gt 3 ]
then
    echo "ok"
else
    echo "nook"
fi

Format 3: if... ; then... ; elif... ; then... ; else... ; fi

#!/bin/bash
a=3
if [ $a -gt 4 ]
then
    echo ">1"
elif [ $a -gt 6 ]
#Note that elif can be nested multiple times
then
    echo "<6 && >1"
else
    echo "nook"
fi

20.6 File Catalog Attribute Judgment

In shell s, it is often necessary to deal with files or directories, so it is very important to judge their attributes.

File Catalog Attribute Judgment

[-f file] determines whether it is a normal file and exists [-f/usr/bin/grep]?
[-d file] determines whether a directory exists and [-d/tmp/mydir]?
[-e file] Determines whether a file or directory exists [-e/var/log/syslog]
[-r file] Determines whether the file is readable [-r/var/log/syslog]
[-w file] Determines whether a file is writable [-w/var/mytmp.txt]
[-x file] Determines whether a file is executable [-x/usr/bin/grep]

Example

#!/bin/bash
f="/tmp/zhouquniclinux"
if [ -e $f ]
then 
       echo $f exist
else
      touch $f

The above example can be simplified to

#!/bin/bash
[ -e $f ] ||  touch $f
[ ! -e $f ] || echo $f exist   
# | | Execute the following commands after the current execution succeeds
# If it is &&, the following command will be executed regardless of whether the previous command is executed successfully or not.
# It's counter-productive.

20.7 if special usage

if special usage

If [- Z'$a], which means what happens when the value of variable a is empty
 If [-n"$a"] means when the value of variable a is not empty
 If grep-q'123'1.txt; then indicates what happens if 1.txt contains rows of'123'.
If [! - e file]; what happens when the N indicates that the file does not exist
 If ($a < 1); then ($a < 1) Equivalent to if [$a-lt 1]; then... 
Symbols such as <,>,==,!=,>=,<== cannot be used in [].

Example

If [- Z "$a"] this means what happens when the value of variable a is null

#!/bin/bash
n='wc -l /tmp/lalala'
if [ $n -lt 100 ]
then
       echo "line num less than 100"
fi
# If the / TMP / Lalalala file is empty or deleted, the script runs incorrectly and has a bug

//A judgment condition should be added.
#!/bin/bash
n='wc -l /tmp/lalala'
if [ $n -z "$n" ]
# [$n-z"$n"]= [!$n-n"$n"], -z and-n are opposite conditions
then
       echo "error"
       exit
elif [ $n -lt 100 ]
then
        echo "line num less than 100"
fi

//perhaps
#!/bin/bash
if [ ! -f /tmp/lalala ]
then
       echo "/tmp/lalala is not exist"
       exit
fi

n='wc -l /tmp/lalala'
if [ $n -lt 100 ]
then
        echo "line num less than 100"
fi

If [-n"$a"] means that when the value of variable a is not empty, it can also judge the file, and it can judge the file without double quotation marks.

if [ -n 01.sh ]; then echo "ok"; fi

//in addition
#!/bin/bash
if [ -n "$b" ]
then  
       echo $b
else
        echo "b is null"
fi

A command can also be used as a condition of judgment. Determine whether user1 users exist

if grep -wq 'user1'  /etc/passwd; then echo "user1 is exist"; else echo "user1 is not exist"; fi
#Grep-w displays filtering information, and-q does not display filtering information

20.8/20.9 case judgement

case Judgment Format

case Variable name in
    value1)
      commond1
      ;;
    value2)
      commod2
      ;;
    value3)
      commod3
      ;;
esac

In case, you can use "|" in the condition to express or mean something, such as:

2|3)
    commond
    ;;

Example: Enter a student's score and judge whether he has passed or not.

[root@localhost sbin]# vim case1.sh
#!/bin/bash
read -p "Please input a number: " n
# Read-p is to read the user's input data and define it in variables.
if [ -z "$n" ]
then
    echo "Please input a number."
    exit 1
#"exit 1" denotes an abnormal operation leading to the exit of the program
#After exit, echo $? Returns a value of 1, indicating that the program exited because of an error.
fi

n1=`echo $n|sed 's/[0-9]//g'`
#Determine whether the character entered by the user is a pure number
#If it's a number, replace it with a null and assign it to $n1
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
#Judging that $n1 is not empty (that is, $n is not a pure number) prompts the user again to enter the number and exit
fi

#If the user enters a pure number, execute the following command:
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
    tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
    tag=2
elif [ $n -ge 80 ]  && [ $n -lt 90 ]
then
    tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
    tag=4
else
    tag=0
fi
#The function of tag is to set a tag for judging conditions, which is convenient for later reference.
case $tag in
    1)
        echo "not ok"
        ;;
    2)
        echo "ok"
        ;;
    3)
        echo "ook"
        ;;
    4)
        echo "oook"
        ;;
    *)
        echo "The number range is 0-100."
        ;;
esac

Operation result

[root@host shell]# sh case1.sh
Please input a number: 45
not ok
[root@host shell]# sh case1.sh
Please input a number: 66
ok
[root@host shell]# sh case1.sh
Please input a number: 80
ook
[root@host shell]# sh case1.sh
Please input a number: 99
oook
[root@host shell]# sh case1.sh
Please input a number: xxx
Please input a number.
[root@host shell]# echo $?
1
[root@host shell]# sh case1.sh
Please input a number: 120
The number range is 0-100.
[root@host shell]# echo $?
0

Posted by tbuzz on Tue, 25 Dec 2018 09:51:06 -0800