码迷,mamicode.com
首页 > 系统相关 > 详细

linux的shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断

时间:2018-02-06 21:33:42      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:tmp   one   方式   add   error   文件的   erro   脚本案例   .sh   

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判断

标签:tmp   one   方式   add   error   文件的   erro   脚本案例   .sh   

原文地址:http://blog.51cto.com/taoxie/2069573

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!