Shell programming (2) -if judgment and special usage, file directory attribute judgment, case judgment

[tap]

Shell programming (2)

1. Logical judgment in shell script

1.1 Judgment statement if

1.1.1 Format 1:

if 判断语句;then 
   command
fi

Example 1

# vim if01.sh          //判断数值大小第一种方法用[],注意前后空格
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
   echo ok
fi
[root@xavilinux ~]# sh if01.sh
ok
  • [ ] -gt: greater than,
  • [ ] -lt: less than,
  • [ ] -ge: greater than or equal to,
  • [ ] -le: less than or equal to,
  • [ ] -eq: equals,
  • [ ] -ne: not equal to

1.1.2 Format 2:

格式:

if 判断语句;then 
   command
else
   command
fi
[root@xavilinux ~]# vim if02.sh
#!/bin/bash
a=2
if [ $a -gt 3 ]
then
   echo ok
else
   echo nook
fi

View the execution process

[root@xavilinux ~]# sh -x if02.sh
+ a=2
+ '[' 2 -gt 3 ']'
+ echo nook
nook

1.1.3 Format 3:

if 判断语句1;then
   command
elif 判断语句2;then
   command
else
   command
fi

Example:

#!/bin/bash
read -p "请输入考试分数:" a
if [ $a -lt 60 ]
then
   echo "重考,未通过考试"
elif [ $a -gt 60 ] && [ $a -lt 85 ]
then
   echo "通过考试,成绩良好"
else
   echo "通过考试,成绩优秀! "
fi
[root@xavilinux ~]# sh if03.sh
请输入考试分数:80
通过考试,成绩良好

[root@xavilinux ~]# sh if03.sh
请输入考试分数:97
通过考试,成绩优秀! 
[root@xavilinux ~]# sh if03.sh
请输入考试分数:54
重考,未通过考试

Second, the file directory attribute judgment

  • [ ] [ -f file ] Determine whether it is a normal file and exists
  • [ ] [ -d file ] Determine whether it is a directory and exists
  • [ ] [ -e file ] Determine whether a file or directory exists
  • [ ] [ -r file ] Determine if the file is readable
  • [ ] [ -w file ] Determine if the file is writable
  • [ ] [ -x file ] Determine if the file is executable
-e filename 如果 filename存在,则为真 [ -e /var/log/syslog ]

-d filename 如果 filename为目录,则为真 [ -d /tmp/mydir ]

-f filename 如果 filename为常规文件,则为真 [ -f /usr/bin/grep ]

-L filename 如果 filename为符号链接,则为真 [ -L /usr/bin/grep ]

-r filename 如果 filename可读,则为真 [ -r /var/log/syslog ]

-w filename 如果 filename可写,则为真 [ -w /var/mytmp.txt ]

-x filename 如果 filename可执行,则为真 [ -L /usr/bin/grep ]
  • Example 1
    #!/bin/bash
    f="/tmp/xavilinux"
    if [ -f $f ]
    then
    echo $f exist
    else
    touch $f
    fi

    Implementation process

[root@xavilinux ~]# sh -x 1.sh
+ f=/tmp/xavilinux
+ '[' -f /tmp/xavilinux ']'
+ touch /tmp/xavilinux
[root@xavilinux ~]# sh -x 1.sh
+ f=/tmp/xavilinux
+ '[' -f /tmp/xavilinux ']'
+ echo /tmp/xavilinux exist
/tmp/xavilinux exist
  • Example 2
#!/bin/bash
f="/tmp/xavilinux"
if [ -r $f ]
then
   echo $f readable

fi
[root@xavi shell]# sh file02.sh
/tmp/xavilinux readable

* Common syntax

[root@xavi shell]# vim file.sh

#!/bin/bash
f="/tmp/xavilinux"
 [ -f $f ] && rm -f $f
等同于以下:

if [ -f $f ]
then
   rm -f $f
fi

3. Special usage of if

if [ -z "$a" ] This means what happens when the value of variable a is empty

To develop a good habit, be sure to add "double quotes" to the value of judgment; if it is "file", it can be omitted

if [ -n "$a" ] means when the value of variable a is not empty

[root@xavi ~]# if [ -n iftest1.sh ]; then echo ok; fi
ok
[root@xavi ~]# if [ -n "$b" ]; then echo $b; else echo
[root@xavi ~]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi
b is null

if grep -q '123' 1.txt; then means what happens if 1.txt contains a line of '123'

grep -ww means word, word
grep -wq plus q does not need to filter users, directly display the results

[root@xavi ~]# grep -w 'xavi' /etc/passwd
xavi:x:1000:1000:xavi,xavi's office,62580558,62589906:/home/xavi:/bin/bash
xavidsf:x:1001:1001:xavi:/home/xavidsf:/bin/bash
[root@xavi ~]# if grep -w 'xavi' /etc/passwd; then echo "xavi exist"; fi
xavi:x:1000:1000:xavi,xavi's office,62580558,62589906:/home/xavi:/bin/bash
xavidsf:x:1001:1001:xavi:/home/xavidsf:/bin/bash
xavi exist
[root@xavi ~]# if grep -wq 'xavi' /etc/passwd; then echo "xavi exist"; fi
xavi exist

if [ ! -e file ]; then what happens when the file does not exist

if (($a<1)); then … is equivalent to if [ $a -lt 1 ]; then…
[ ] The symbols <,>,==,!=,>=,<= cannot be used

Four, case judgment

4.1 Format

case  变量名 in 
value1)
  command
  ;;
value2)
  command
  ;;
*)
  commond
  ;;
esac

If a value in the case is the same, we can write:

在case程序中,可以在条件中使用 |,表示或的意思,  
比如

2|3)     
    command  
    ;;

Script case

#!/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 just input a number, without any other words."
 exit 1
fi

if [ $n -lt 60 ] && [ $n -ge 0 ]        //经过如上的筛选,我们来判断输入数字属于哪个范围,并且把值交给tag
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  //大于100的情况
fi
case $tag in        //根据如上得到的值,进行判断
    1)
    echo "you didn't pass the exam!"
        ;;
    2)
        echo "good!"
        ;;
    3)
        echo "very good!"
        ;;
    4)
        echo "perfect!!!"
        ;;
    *)
        echo "Pls input a number range 0-100."
        ;; 
esac

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324606683&siteId=291194637