If logic judgments in shell scripts, file directory attribute judgments, if special usages, case judgments

Source: Internet
Author: User
Tags syslog
if logic judgment in shell scripts


Logical judgment Expression:


-GT (>);    Greater than    great than
-lt (<);      Less     than
-ge (>=);  Greater than or equal to   
-le (<=);   Less than or equal to
-eq (= =);  equals     equal
-ne (! =)    is not equal to not Equa--

such as
if [$a-gt $b]; 
If [$a-lt 5]; 
If [$b-eq 10] etc.


If logical judgment format:


Format 1:if condition; Then statement; Fi
format 2:if condition; then statement; Else statement; Fi
format 3:if ...; Elif ...; Then ...; else ...; Fi
--

can use && | | Combine multiple conditions
a&& conditions b:a and b
conditions a| | Condition b:a or b
if [$a-gt 5] && [$a-lt]; then
if [$b-gt 5] | | [$b-lt 3]; Then


Format 1:if condition; Then statement; Fi


#!/bin/bash
a=5
If [$a-gt 3]
#注意 [] There are plenty of spaces 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]
#注意elif可以嵌套多次的
then
    E Cho "<6 && >1"
else
    echo "Nook"
fi
file directory property judgment


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



File Directory property judgment


[-F file] Determines whether the file is normal, and there is [     -f/usr/bin/grep]
[-D file] to determine whether it is a directory, and there is   [-d/tmp/mydir]
[-e File] judgment Whether the file or directory exists   [-e/var/log/syslog] [-R File] to determine whether the document is readable   [-r/var/log/syslog]
[-W file] to determine if the file is writable[-w/var/mytmp.txt]
[x file] Determines whether the file can be executed  [-x/usr/bin/grep]


Example:


#!/bin/bash
f= "/tmp/zhouquniclinux"
if [-e $f] then 
       echo $f exist
else
      touch $f
Fi
If special usage
If [-Z ' $a ']  This indicates what happens if the value of variable A is empty if
[-n ' $a "]  means that the value of variable A is not NULL if
grep-q ' 123 ' 1.txt; then  indicates if 1.txt contains ' What happens when 123 ' is the
if [!-e file]; then indicates what happens if the file does not exist
if (($a <1)); then ... 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 empty

#!/bin/bash
n= ' wc-l/tmp/lalala '
if [$n-lt] then
       echo "Lin E num less than "
fi
# If the/tmp/lalala file is empty, or if it is deleted, the script will run an error, and

a bug should be added with a judgment condition
#!/bin/bash
n= ' wc-l/tmp/lalala '
if [$n-Z ' $n]
# [$n-Z "$n"]  = [! $n-N "$n"],-z and-N are a pair of opposite conditions
thenecho "Error"
       exit
elif [$n-lt] then
        echo "line num less than"
fi

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

n= ' wc-l/tmp/la Lala '
if [$n-lt] then
        echo "line num less than"
fi
Case Judgment


Case Judgment Format


Case variable name in
    value1)
      commond1
      ;;
    value2)
      commod2
      ;;
    VALUE3)
      commod3
      ;;
Esac


In the NIC system service script, for example, the case is used in/etc/init.d/iptables



In a case, you can use "|" in the condition, meaning that


Enter a classmate's score to determine whether the grade is satisfactory and excellent.


#! / bin / bash
read -p "Please input a number:" n
# read -p is to read the user's input data and define it into a variable
if [-z "$ n"]
then
    echo "Please input a number."
    exit 1
# "Exit 1" means the program exited abnormally
After exiting, echo $? Will return a value of 1, indicating that the program exited because of an error, which is the same as when viewing the previous command for errors.
fi

n1 = `echo $ n | sed 's / [0-9] // g'`
#Determine whether the characters entered by the user are pure numbers
#If it is a number, replace it with null and assign it to $ n1
if [-n "$ n1"]
then
echo "Please input a number."
exit 1
#When $ n1 is not empty (that is, $ n is not a pure number), the user is prompted again to enter a 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 role of #tag is to set tags for judgment 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 
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.