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

一、shell脚本中的逻辑判断
shell脚本中的逻辑判断  文件目录属性判断  if特殊用法  case判断
在shell脚本中,很多都会逻辑判断,判断某一个数值,判断某一个文件,或者某一个目录,我们针对判断结果再做一些操作,如果没有判断,就没法做一些操作
格式1:if条件;then语句;fi
例子:
[root@linux-01 ~]# if [ $a -ge 3 ] //分行写就是这样写

then
echo ok
fi
ok
[root@linux-01 ~]# if [ $a -ge 3 ]; then echo ok; fi //这是一行写的格式
//解释:-gt表示大于的意思,格式比较特殊:方括号内的两边必须都要有空格,-gt的两边必须都要有空格,条件语句到处都是空格
我们可以把上面的这条命令写成脚本:
[root@linux-01 ~]# cd shell/
[root@linux-01 shell]# vi if1.sh
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
echo ok
fi
[root@linux-01 shell]# sh if1.sh //执行脚本,查看脚本执行结果
ok
大部分的脚本都使用这个格式,都使用逻辑判断if ,then

格式2:if条件;then语句;else语句;fi
例子:
[root@linux-01 shell]# cp if1.sh if2.sh
[root@linux-01 shell]# vi if2.sh
#!/bin/bash
a=1
if [ $a -gt 3 ]
then
echo ok
else
echo nook
fi
[root@linux-01 shell]# sh if2.sh //执行增加了else语句的判断,输出nook
nook
[root@linux-01 shell]# sh -x if2.sh //-x可以查看脚本的详细执行过程

  • a=1
  • '[' 1 -gt 3 ']' //1和3比较不是大于3的,判断是else
  • echo nook
    nook

格式3:if...;then...;elif...;then...;else...;fi
例子:
[root@linux-01 shell]# cp if2.sh if3.sh
[root@linux-01 shell]# vi if3.sh
#!/bin/bash
a=5
if [ $a -gt 1 ] //如果a大于1
then
echo ">1" //输出>1
elif [ $a -lt 6 ] //在大于1的基础上小于6,elif这个条件判断可以写多次
then
echo "<6 && >1" //输出<6 && >1
else //else表示除了上面两个条件之外的其他的条件了
echo nook
fi
[root@linux-01 shell]# sh -x if3.sh //使用-x选项查看脚本详细执行过程

  • a=5
  • '[' 5 -gt 1 ']'
  • echo '>1'

    1
    上面这个if3.sh这个脚本的逻辑可能有问题,需要改进
    逻辑判断表达式:
    if [ $a -gt $b ] 表示如果a>b;
    if [ $a -lt 5 ]表示如果a<5;
    if [ $b -eq 10 ]表示如果b=10;
    if [ $b -ne 10 ]表示如果b不=10;
    -gt(>)
    -lt(<)
    -ge(>=)
    -le(<=)
    -eq(==)
    -ne(!=)
    如果想直接使用大于号小于号,可以换种格式去写
    [root@linux-01 shell]# a=3
    [root@linux-01 shell]# if (($a>1)); then echo ok; fi //使用两个括号,但是使用起来繁琐
    ok
    可以使用&& || 结合多个条件,&&表示并且,||表示或者
    if [ $a -gt 5 ] && [ $a -lt 10 ]; then //如果a大于5并且小于10,then
    if [ $b -gt 5 ] || [ $b -lt 3 ]; then //如果b大于5或者小于3,then

二、文件目录属性判断
shell脚本中的逻辑判断  文件目录属性判断  if特殊用法  case判断
1、[ -f file ]判断文件是否是普通文件,并且存在
实例:创建file1.sh脚本
[root@linux-01 shell]# vi file1.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -f $f ]
then
echo $f exist
else
touch $f
fi
[root@linux-01 shell]# sh -x file1.sh //查看脚本执行过程

  • f=/tmp/aminglinux //查看文件路径
  • '[' -f /tmp/aminglinux ']' //去比较文件存不存在
  • touch /tmp/aminglinux //不存在就去创建
    [root@linux-01 shell]# sh -x file1.sh //再次执行脚本提示文件已经存在
  • f=/tmp/aminglinux
  • '[' -f /tmp/aminglinux ']'
  • echo /tmp/aminglinux exist
    /tmp/aminglinux exist

2、[ -d file ]判断文件是否是目录,并且存在
实例:
[root@linux-01 shell]# cp file1.sh file2.sh //拷贝下file1,sh脚本去修改
[root@linux-01 shell]# vi file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -d $f ] //这里修改为-d,判断是不是
then
echo $f exist
else
touch $f
fi
[root@linux-01 shell]# sh -x file2.sh

  • f=/tmp/aminglinux
  • '[' -d /tmp/aminglinux ']' //比较不是目录
  • touch /tmp/aminglinux //创建目录

3、[ -e file ]判断文件或目录是否存在
实例;
[root@linux-01 shell]# vi file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -e $f ] //-e判断它不管是文件或者目录
then
echo $f exist
else
touch $f
fi
[root@linux-01 shell]# sh -x file2.sh

  • f=/tmp/aminglinux
  • '[' -e /tmp/aminglinux ']'
  • echo /tmp/aminglinux exist
    /tmp/aminglinux exist
    //目录和文件都是可以touch的,如果touch的时候这个文件或者目录是存在的,那么可以更改文件的三个time,分别是atime,ctime,mtime

4、[ -r file ]判断文件是否可读
实例:
[root@linux-01 shell]# vi file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -r $f ]
then
echo $f readable
fi
[root@linux-01 shell]# sh file2.sh //执行脚本,提示可读
/tmp/aminglinux readable

5、[ -w file ]判断文件是否可写
实例:
[root@linux-01 shell]# vi file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -w $f ]
then
echo $f writeable
fi
[root@linux-01 shell]# sh file2.sh 执行脚本,提示可写
/tmp/aminglinux writeable
那它判断可读可写实际上是判断执行这个shell脚本的当前用户,我们是以root的身份去执行的脚本

6、 [ -x file ]判断文件是否可执行
实例:
[root@linux-01 shell]# vi file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -x $f ]
then
echo $f exeable
fi
[root@linux-01 shell]# sh file2.sh //因为没有执行的权限,我们的脚本也没有定义else,所以没有任何输出信息

下面对这个脚本进行改良
在工作中使用最多的是:通常先判断文件存在不存在,如果存在,就会删除这个文件,&&表示当前面的命令执行成功以后才会去执行后面的命令
那么[ -f $f ] && rm -f $f这一行命令等同于下面这四行命令
if [ -f $f ]
then
rm -f $f
fi
[ -f $f ] || touch $f 表示如果$f文件执行不成功的时候,才执行后面的touch $f命令
[ -f $f ] || touch $f 等同于下面这四行
if [ ! -f $f ]
then
touch $f
fi

三、if特殊用法
shell脚本中的逻辑判断  文件目录属性判断  if特殊用法  case判断
1、if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
例子:
[root@linux-01 shell]# vi if4.sh
#!/bin/bash
n=wc -l /tmp/lalal
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo abcdef
fi
//if [ -z "$n" ]表示当变量n的值为空的时候
[root@linux-01 shell]# sh -x if4.sh //执行脚本,查看执行过程
++ wc -l /tmp/lalal
wc: /tmp/lalal: No such file or directory

  • n=
  • '[' -z '' ']'
  • echo error
    error
  • exit

对if4.sh脚本再次完善:
[root@linux-01 shell]# vi 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 abcdef
fi
[root@linux-01 shell]# sh if4.sh //执行脚本,如果/tmp/lalal文件不存在,就直接退出脚本
/tmp/lalal not exist.

2、if [ -n "$a" ] 表示当变量a的值不为空
实例:
[root@linux-01 shell]# if [ -n 01.sh ]; then echo ok; fi //如果01.sh不为空,输出ok
ok
[root@linux-01 shell]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi
b is null
//如果变量b值不为空,输出$b,否则输出"b is null"
注意,如果[ -n 01.sh ]的是文件,不需要使用引号,如果 [ -n "$b" ]中的是变量,需要使用引号引起来

3、if grep -q '123' 1.txt; then 表示如果1.txt中含有‘123’的行时怎么样
例子:
需求:在脚本中,可以使用一个命令的结果作为判断条件,判断某一个文件中是否含有某一个字符串,比如判断系统中的用户里面是否有user1这个用户,我们可以使用如下命令去判断
[root@linux-01 shell]# grep -w 'user1' /etc/passwd //-w选项可以更加精准的去匹配
user1:x:1000:1000::/home/user1:/bin/bash
有了这样的思路,可以这样去判断
[root@linux-01 shell]# if grep -wq 'user1' /etc/passwd; then echo "user1 exist"; fi //-q选项可以不显示过滤掉的内容
user1 exist

4、if [ ! -e file ]; then 表示文件不存在时会怎么样
5、if (($a<1)); then... 等同于 if [ $a -lt 1 ]; then...
6、[ ] 中不能使用<,>,==,!=,>=,<=这样的符号

四、case判断
shell脚本中的逻辑判断  文件目录属性判断  if特殊用法  case判断
双分号表示第一个判断结束,进入下一个判断
Shell脚本实例:
[root@linux-01 shell]# vi case.sh
#!/bin/bash
read -p "Please input a number: " n //请用户输入一个数字,n作为一个捕获的变量用户输入什么输出什么值
if [ -z "$n" ] //如果n的值为空,请输入一个数字
then
echo "Please input a number."
exit 1 //退出来捕获变量1的值
fi

n1=echo $n|sed 's/[0-9]//g' //判断输入的数字是否含有字母或全是字母,使用sed把所有数字置空
if [ ! -z $n1 ] //这一行或者使用if [ -n "$n1" ]代替,如果判断不为空,退出来,提示用户输入一个数字
then
echo "Please input a number."
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

转载于:https://blog.51cto.com/13669226/2143908

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值