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

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

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 ,thenshell

格式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能够查看脚本的详细执行过程bash

  • 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选项查看脚本详细执行过程ide

  • 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,then3d

2、文件目录属性判断
shell脚本中的逻辑判断  文件目录属性判断  if特殊用法  case判断
一、[ -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 //查看脚本执行过程code

  • 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

二、[ -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 blog

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

三、[ -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

四、[ -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 readableinput

五、[ -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的身份去执行的脚本it

六、 [ -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

3、if特殊用法
shell脚本中的逻辑判断  文件目录属性判断  if特殊用法  case判断
一、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.

二、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" ]中的是变量,须要使用引号引发来

三、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

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

4、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 ] //若是是数字,须要判断数字的范围thentag=1elif [ $n -ge 60 ] && [ $n -lt 80 ]thentag=2elif [ $n -ge 80 ] && [ $n -lt 90 ]thentag=3elif [ $n -ge 90 ] && [ $n -le 100 ]thentag=4elsetag=0ficase $tag in1)echo "not ok";;2)echo "ok";;3)echo "ook";;4)echo "oook";;*)echo "The number range is 0-100.";;esac

相关文章
相关标签/搜索