1. 程式人生 > >linux的shell腳本中的邏輯判斷、文件目錄屬性判斷、if特殊用法、case判斷

linux的shell腳本中的邏輯判斷、文件目錄屬性判斷、if特殊用法、case判斷

例子 add 輸出 文件目錄屬性判斷 glin 如果 work ada shell腳本

Shell腳本中的邏輯判斷
  • 格式1:if 條件 ; then 語句; fi (常用)
#以命令的方式表達
[root@garytao-01 ~]# for i in `seq 1 5`; do echo $i; done
1
2
3
4
5
[root@garytao-01 ~]# for i in `seq 1 5`
> do
> echo $i
> done
1
2
3
4
5
[root@garytao-01 ~]# a=5
[root@garytao-01 ~]# if [ $a -gt 3 ]
> then
> echo ok
> fi
ok
[root@garytao-01 ~]# if [ $a -gt 3 ]; then echo ok; fi
ok

#腳本執行
[root@garytao-01 ~]# cd shell/
[root@garytao-01 shell]# vi if1.sh
[root@garytao-01 shell]# cat if1.sh 
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
    echo ok
fi
[root@garytao-01 shell]# sh if1.sh 
ok
  • 格式2:if 條件; then 語句; else 語句; fi
[root@garytao-01 shell]# cp if1.sh if2.sh
[root@garytao-01 shell]# vi if2.sh 
[root@garytao-01 shell]# cat if2.sh 
#!/bin/bash
a=1
if [ $a -gt 3 ]
then
    echo ok
else
    echo nook
fi
[root@garytao-01 shell]# sh -x if2.sh 
+ a=1
+ ‘[‘ 1 -gt 3 ‘]‘
+ echo nook
nook
[root@garytao-01 shell]# sh if2.sh 
nook
  • 格式3:if …; then … ;elif …; then …; else …; fi
[root@garytao-01 shell]# cp if2.sh if3.sh
[root@garytao-01 shell]# vi if3.sh 
[root@garytao-01 shell]# cat if3.sh 
#!/bin/bash
a=3
if [ $a -gt 4 ]
then
    echo ">1"
elif [ $a -gt 6 ]
then
    echo "<6 && >1"
else
    echo nook
fi
[root@garytao-01 shell]# sh -x if3.sh 
+ a=3
+ ‘[‘ 3 -gt 4 ‘]‘
+ ‘[‘ 3 -gt 6 ‘]‘
+ echo nook
nook
[root@garytao-01 shell]# sh if3.sh 
nook
  • 邏輯判斷表達式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 註意到處都是空格

  • 可以使用 && || 結合多個條件
    if [ $a -gt 5 ] && [ $a -lt 10 ]; then
    if [ $b -gt 5 ] || [ $b -lt 3 ]; then

文件目錄屬性判斷

[ -f file ]判斷是否是普通文件,且存在
[ -d file ] 判斷是否是目錄,且存在
[ -e file ] 判斷文件或目錄是否存在
[ -r file ] 判斷文件是否可讀
[ -w file ] 判斷文件是否可寫
[ -x file ] 判斷文件是否可執行

[root@garytao-01 shell]# vi filel.sh
[root@garytao-01 shell]# cat filel.sh 
#!/bin/bash
f="/tmp/aminglinux"
if [ -f $f ]
then 
    echo $f exist
else
    touch $f
fi
[root@garytao-01 shell]# sh -x filel.sh 
+ f=/tmp/aminglinux
+ ‘[‘ -f /tmp/aminglinux ‘]‘
+ touch /tmp/aminglinux
[root@garytao-01 shell]# sh -x filel.sh 
+ f=/tmp/aminglinux
+ ‘[‘ -f /tmp/aminglinux ‘]‘
+ echo /tmp/aminglinux exist
/tmp/aminglinux exist
[root@garytao-01 shell]# cp filel.sh filel2.sh
[root@garytao-01 shell]# vi filel2.sh 
[root@garytao-01 shell]# cat filel2.sh 
#!/bin/bash
f="/tmp/aminglinux"
if [ -d $f ]
then 
    echo $f exist
else
    touch $f
fi
[root@garytao-01 shell]# sh -x filel2.sh 
+ f=/tmp/aminglinux
+ ‘[‘ -d /tmp/aminglinux ‘]‘
+ touch /tmp/aminglinux
[root@garytao-01 shell]# vi filel2.sh 
[root@garytao-01 shell]# cat filel2.sh 
#!/bin/bash
f="/tmp/aminglinux"
if [ -e $f ]
then 
    echo $f exist
else
    touch $f
fi
[root@garytao-01 shell]# sh -x filel2.sh 
+ f=/tmp/aminglinux
+ ‘[‘ -e /tmp/aminglinux ‘]‘
+ echo /tmp/aminglinux exist
/tmp/aminglinux exist

[root@garytao-01 shell]# vi filel2.sh
[root@garytao-01 shell]# cat filel2.sh 
#!/bin/bash
f="/tmp/aminglinux"
if [ -r $f ]
then 
    echo $f readable
fi
[root@garytao-01 shell]# sh -x filel2.sh 
+ f=/tmp/aminglinux
+ ‘[‘ -r /tmp/aminglinux ‘]‘
+ echo /tmp/aminglinux readable
/tmp/aminglinux readable
[root@garytao-01 shell]# vi filel2.sh
[root@garytao-01 shell]# cat filel2.sh 
#!/bin/bash
f="/tmp/aminglinux"
if [ -w $f ]
then 
    echo $f writeable
fi
[root@garytao-01 shell]# sh -x filel2.sh 
+ f=/tmp/aminglinux
+ ‘[‘ -w /tmp/aminglinux ‘]‘
+ echo /tmp/aminglinux writeable
/tmp/aminglinux writeable
root@garytao-01 shell]# ls -l /tmp/aminglinux 
-rw-r--r-- 1 root root 0 Feb  3 14:52 /tmp/aminglinux
[root@garytao-01 shell]# vi filel2.sh
[root@garytao-01 shell]# cat filel2.sh 
#!/bin/bash
f="/tmp/aminglinux"
if [ -x $f ]
then 
    echo $f exeable
fi
[root@garytao-01 shell]# sh filel2.sh  //因為不可執行,所以沒有輸出

if特殊用法

  • if [ -z "$a" ]??這個表示當變量a的值為空時會怎麽樣
[root@garytao-01 shell]# vi if4.sh
[root@garytao-01 shell]# sh -x if4.sh 
++ wc -l /tmp/lalal
wc: /tmp/lalal: 沒有那個文件或目錄
+ n=
+ ‘[‘ -gt 100 ‘]‘
if4.sh: 第 3 行:[: -gt: 期待一元表達式
[root@garytao-01 shell]# vi if4.sh
[root@garytao-01 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
[root@garytao-01 shell]# sh -x if4.sh 
++ wc -l /tmp/lalal
wc: /tmp/lalal: 沒有那個文件或目錄
+ n=
+ ‘[‘ -z ‘‘ ‘]‘
+ echo error
error
+ exit
[root@garytao-01 shell]# vi if4.sh
[root@garytao-01 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
[root@garytao-01 shell]# sh -x if4.sh 
+ ‘[‘ ‘!‘ -f /tmp/lalal ‘]‘
+ echo ‘/tmp/lalal not exist.‘
/tmp/lalal not exist.
+ exit
[root@garytao-01 shell]# sh if4.sh 
/tmp/lalal not exist.
  • if [ -n "$a" ] 表示當變量a的值不為空
[root@garytao-01 shell]# ls
01.sh  filel2.sh  filel.sh  for1.sh  if1.sh  if2.sh  if3.sh  if4.sh
[root@garytao-01 shell]# if [ -n 01.sh ]; then echo ok; fi
ok
[root@garytao-01 shell]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi
b is null
  • if grep -q ‘123‘ 1.txt; then??表示如果1.txt中含有‘123‘的行時會怎麽樣
  • if [ ! -e file ]; then 表示文件不存在時會怎麽樣
  • if (($a<1)); then …等同於 if [ $a -lt 1 ]; then…
  • [ ] 中不能使用<,>,==,!=,>=,<=這樣的符號
[root@garytao-01 shell]# grep -w ‘user1‘ /etc/passwd
user1:x:1002:1002::/home/user1:/bin/bash
[root@garytao-01 shell]# if grep -w ‘user1‘ /etc/passwd; then echo "user1 exist"; fi
user1:x:1002:1002::/home/user1:/bin/bash
user1 exist
[root@garytao-01 shell]# if grep -wq ‘user1‘ /etc/passwd; then echo "user1 exist"; fi
user1 exist
[root@garytao-01 shell]# if ! grep -wq ‘user1‘ /etc/passwd; then useradd user1; fi
[root@garytao-01 shell]# 

case判斷

  • 可以查看vi /etc/init.d/network文件的case判斷例子

技術分享圖片

  • case判斷腳本格式

技術分享圖片

  • shell腳本案例
[root@garytao-01 shell]# vi case.sh
[root@garytao-01 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

[root@garytao-01 shell]# sh case.sh 
Please input a number: 101
The number range is 0-100.
[root@garytao-01 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.

linux的shell腳本中的邏輯判斷、文件目錄屬性判斷、if特殊用法、case判斷