Logical judgments in Linux shell scripts, file directory attribute judgments, if special usages, case judgments

Source: Internet
Author: User


Logical judgments in shell scripts
    • Format 1:if condition; Then statement; Fi (Common)
#Expressed by command
[[email protected] ~] # for i in `seq 1 5`; do echo $ i; done
1
2
3
4
5
[[email protected] ~] # for i in `seq 1 5`
> do
> echo $ i
> done
1
2
3
4
5
[[email protected] ~] # a = 5
[[email protected] ~] # if [$ a -gt 3]
> then
> echo ok
> fi
ok
[[email protected] ~] # if [$ a -gt 3]; then echo ok; fi
ok

#Script execution
[[email protected] ~] # cd shell /
[[email protected] shell] # vi if1.sh
[[email protected] shell] # cat if1.sh
#! / bin / bash
a = 5
if [$ a -gt 3]
then
     echo ok
fi
[[email protected] shell] # sh if1.sh
ok
    • Format 2:if condition; Then statement; else statement; Fi
[[email protected] shell]# cp if1.sh if2.sh
[[email protected] shell]# vi if2.sh 
[[email protected] shell]# cat if2.sh 
#!/bin/bash
a=1
if [ $a -gt 3 ]
then
    echo ok
else
    echo nook
fi
[[email protected] shell]# sh -x if2.sh 
+ a=1
+ ‘[‘ 1 -gt 3 ‘]‘
+ echo nook
nook
[[email protected] shell]# sh if2.sh 
nook
    • Format 3:if ...; Then ...; Elif ...; Then ...; else ...; Fi
[[email protected] shell] # vi filel.sh
[[email protected] shell] # cat filel.sh
#! / bin / bash
f = "/ tmp / aminglinux"
if [-f $ f]
then
    echo $ f exist
else
    touch $ f
fi
[[email protected] shell] # sh -x filel.sh
+ f = / tmp / aminglinux
+ ‘[’ -F / tmp / aminglinux ‘]’
+ touch / tmp / aminglinux
[[email protected] shell] # sh -x filel.sh
+ f = / tmp / aminglinux
+ ‘[’ -F / tmp / aminglinux ‘]’
+ echo / tmp / aminglinux exist
/ tmp / aminglinux exist
[[email protected] shell] # cp filel.sh filel2.sh
[[email protected] shell] # vi filel2.sh
[[email protected] shell] # cat filel2.sh
#! / bin / bash
f = "/ tmp / aminglinux"
if [-d $ f]
then
    echo $ f exist
else
    touch $ f
fi
[[email protected] shell] # sh -x filel2.sh
+ f = / tmp / aminglinux
+ ‘[’ -D / tmp / aminglinux ‘]’
+ touch / tmp / aminglinux
[[email protected] shell] # vi filel2.sh
[[email protected] shell] # cat filel2.sh
#! / bin / bash
f = "/ tmp / aminglinux"
if [-e $ f]
then
    echo $ f exist
else
    touch $ f
fi
[[email protected] shell] # sh -x filel2.sh
+ f = / tmp / aminglinux
+ ‘[‘ -E / tmp / aminglinux ‘]’
+ echo / tmp / aminglinux exist
/ tmp / aminglinux exist

[[email protected] shell] # vi filel2.sh
[[email protected] shell] # cat filel2.sh
#! / bin / bash
f = "/ tmp / aminglinux"
if [-r $ f]
then
    echo $ f readable
fi
[[email protected] shell] # sh -x filel2.sh
+ f = / tmp / aminglinux
+ ‘[‘ -R / tmp / aminglinux ‘]’
+ echo / tmp / aminglinux readable
/ tmp / aminglinux readable
[[email protected] shell] # vi filel2.sh
[[email protected] shell] # cat filel2.sh
#! / bin / bash
f = "/ tmp / aminglinux"
if [-w $ f]
then
    echo $ f writeable
fi
[[email protected] shell] # sh -x filel2.sh
+ f = / tmp / aminglinux
+ ‘[’ -W / tmp / aminglinux ‘]’
+ echo / tmp / aminglinux writeable
/ tmp / aminglinux writeable
[email protected] shell] # ls -l / tmp / aminglinux
-rw-r--r-- 1 root root 0 Feb 3 14:52 / tmp / aminglinux
[[email protected] shell] # vi filel2.sh
[[email protected] shell] # cat filel2.sh
#! / bin / bash
f = "/ tmp / aminglinux"
if [-x $ f]
then
    echo $ f exeable
fi
[[email protected] shell] # sh filel2.sh // No output because it is not executable
    • Logical judgment expression: if [$a-gt $b]; If [$a-lt 5]; If [$b-eq 10] et-gt (>); -lt (<); -ge (>=); -le (<=);-eq (= =); -ne (! =) Note that there are spaces everywhere

    • can use && | | Combine multiple conditions
      If [$a-gt 5] && [$a-lt 10]; Then
      If [$b-gt 5] | | [$b-lt 3]; Then
File Directory property judgment


[-F file] Determines if it is a normal file, and there is
[-D file] Determines if it is a directory and exists
[-E file] to determine whether files or directories exist
[-R File] to determine if the document is readable
[-W file] Determines whether the file is writable
[-X file] Determines whether the file is executable


[[email protected] shell] # vi if4.sh
[[email protected] shell] # sh -x if4.sh
++ wc -l / tmp / lalal
wc: / tmp / lalal: no such file or directory
+ n =
+ ‘[’ -Gt 100 ‘]’
if4.sh: line 3: [: -gt: expect unary expression
[[email protected] shell] # vi if4.sh
[[email protected] shell] # cat if4.sh
#! / bin / bash
n = `wc -l / tmp / lalal`
if [-z "$ n"]
then
    echo error
    exit
elif [$ n -gt 100]
then
    echo aladafaf
fi
[[email protected] shell] # sh -x if4.sh
++ wc -l / tmp / lalal
wc: / tmp / lalal: no such file or directory
+ n =
+ ‘[’ -Z ‘‘ ‘]’
+ echo error
error
+ exit
[[email protected] shell] # vi if4.sh
[[email protected] shell] # cat if4.sh
#! / bin / bash
if [! -f / tmp / lalal]
then
    echo "/ tmp / lalal not exist."
    exit
fi
n = `wc -l / tmp / lalal`
if [-z "$ n"]
then
    echo error
    exit
elif [$ n -gt 100]
then
    echo alsdflljk
fi
[[email protected] shell] # sh -x if4.sh
+ ‘[’ ‘!’ -F / tmp / lalal ‘]’
+ echo ‘/ tmp / lalal not exist.’
/ tmp / lalal not exist.
+ exit
[[email protected] shell] # sh if4.sh
/ tmp / lalal not exist.

If special usage
    • If [-Z "$a"]?? This indicates what happens when the value of variable A is empty
[[email protected] shell]# ls
01.sh  filel2.sh  filel.sh  for1.sh  if1.sh  if2.sh  if3.sh  if4.sh
[[email protected] shell]# if [ -n 01.sh ]; then echo ok; fi
ok
[[email protected] shell]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi
b is null

    • If [-n ' $a '] means that the value of variable A is not empty
[[email protected] shell]# grep -w ‘user1‘ /etc/passwd
user1:x:1002:1002::/home/user1:/bin/bash
[[email protected] shell]# if grep -w ‘user1‘ /etc/passwd; then echo "user1 exist"; fi
user1:x:1002:1002::/home/user1:/bin/bash
user1 exist
[[email protected] shell]# if grep -wq ‘user1‘ /etc/passwd; then echo "user1 exist"; fi
user1 exist
[[email protected] shell]# if ! grep -wq ‘user1‘ /etc/passwd; then useradd user1; fi
[[email protected] shell]#
    • If Grep-q ' 123 ' 1.txt; Then?? What happens if the 1.txt contains a ' 123 ' row
    • if [!-e file]; Then what happens when the file doesn't exist?
    • if (($a <1)); Then ... Equivalent to if [$a-lt 1]; Then ...
    • Symbols such as <,>,==,!=,>=,<= cannot be used in []

Case Judgment
    • Example of case judgment for Vi/etc/init.d/network file can be viewed




    • Case Judgment Script format




    • Shell Script case
[[email protected] shell]# vi case.sh
[[email protected] shell]# cat case.sh 

#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
    echo "Please input a number."
    exit 1
fi

n1=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$n1" ]
then
    echo "Please input a number."
    exit 1
#elif [ $n -lt 0 ] || [ $n -gt 100 ]
#then
#    echo "The number range is 0-100."
#    exit 1
fi

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

case $tag in
    1)
    echo "not ok"
        ;;
    2)
        echo "ok"
        ;;
    3)
        echo "ook"
        ;;
    4)
        echo "oook"
        ;;
    *)
        echo "The number range is 0-100."
        ;; 
esac

[[email protected] shell]# sh case.sh 
Please input a number: 101
The number range is 0-100.
[[email protected] shell]# sh -x case.sh 
+ read -p ‘Please input a number: ‘ n
Please input a number: 101
+ ‘[‘ -z 101 ‘]‘
++ echo 101
++ sed ‘s/[0-9]//g‘
+ n1=
+ ‘[‘ -n ‘‘ ‘]‘
+ ‘[‘ 101 -lt 60 ‘]‘
+ ‘[‘ 101 -ge 60 ‘]‘
+ ‘[‘ 101 -lt 80 ‘]‘
+ ‘[‘ 101 -ge 80 ‘]‘
+ ‘[‘ 101 -lt 90 ‘]‘
+ ‘[‘ 101 -ge 90 ‘]‘
+ ‘[‘ 101 -le 100 ‘]‘
+ tag=0
+ case $tag in
+ echo ‘The number range is 0-100.‘
The number range is 0-100.


Logical judgments in shell scripts for Linux, file directory attribute judgments, if special usages, case judgments


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.