shell脚本中的逻辑判断

shell脚本中的逻辑判断

if 逻辑判断。在shell中if判断的基本语法为:

1)不带else

if  判断语句; then
    command
fi

例如:

[[email protected] sbin]# cat if1.sh
#! /bin/bash

read -p "Please input your score: " a
if (($a<60)); then
    echo "You didn‘t pass the exam."
fi

在if1.sh中出现了 (($a<60)) 这样的形式,这是shell脚本中特有的格式,用一个小括号或者不用都会报错,请记住这个格式或者可以写为[ $a lt 60 ]。执行结果为:

[[email protected] sbin]# sh if1.sh
Please input your score: 90
[[email protected] sbin]# sh if1.sh
Please input your score: 33
You didn‘t pass the exam.

2)带有else

if  判断语句  ; then
    command
else
    command
fi

例如:

[[email protected] sbin]# cat if2.sh
#! /bin/bash

read -p "Please input your score: " a
if (($a<60)); then
     echo "You didn‘t pass the exam."
else
     echo "Good! You passed the exam."
fi

执行结果:

[[email protected] sbin]# sh if2.sh
Please input your score: 80
Good! You passed the exam.
[[email protected] sbin]# sh if2.sh
Please input your score: 25
You didn‘t pass the exam.

和上一例唯一区别的地方是,如果输入大于等于60的数字会有所提示。

3)带有elif

if  判断语句一  ; then
    command
elif  判断语句二; then
    command
else
    command
fi

例如:

[[email protected] sbin]# cat if3.sh
#! /bin/bash

 read -p "Please input your score: " a
 if (($a<60)); then
         echo "You didn‘t pass the exam."
 elif (($a>=60)) && (($a<85)); then
         echo "Good! You pass the exam."
 else
         echo "very good! Your socre is very high!"
 fi

这里的 && 表示 “并且” 的意思,当然也可以使用 || 表示 “或者” 执行结果为:

[[email protected] sbin]# sh if3.sh
Please input your score: 90
very good! Your socre is very high!
[[email protected] sbin]# sh if3.sh
Please input your score: 60
Good! You pass the exam.

以上只是简单的介绍了if语句的结构。在判断数值大小除了可以用 (( )) 的形式外,还可以使用 [ ] 但是就不能使用>, < , = 这样的符号了,要使用 -lt (小于),-gt (大于),-le (小于等于),-ge (大于等于),-eq (等于),-ne (不等于)。

[[email protected] sbin]# a=10; if [ $a -lt 5 ]; then echo ok; fi
[[email protected] sbin]# a=10; if [ $a -gt 5 ]; then echo ok; fi
ok
[[email protected] sbin]# a=10; if [ $a -ge 10 ]; then echo ok; fi
ok
[[email protected] sbin]# a=10; if [ $a -eq 10 ]; then echo ok; fi
ok
[[email protected] sbin]# a=10; if [ $a -ne 10 ]; then echo ok; fi

再看看if中使用 && 和 ||的情况:

[[email protected] sbin]# a=10; if [ $a -lt 1 ] || [ $a -gt 5 ]; then echo ok; fi
ok
[[email protected] sbin]# a=10; if [ $a -gt 1 ] || [ $a -lt 10 ]; then echo ok; fi
ok

shell 脚本中if还经常判断关于档案属性,比如判断是普通文件还是目录,判断文件是否有读写执行权限等。常用的也就几个选项:

-e :判断文件或目录是否存在

-d :判断是不是目录,并是否存在

-f :判断是否是普通文件,并存在

-r :判断文档是否有读权限

-w :判断是否有写权限

-x :判断是否可执行

使用if判断时,具体格式为:

if [ -e filename ] ; then

例子:

[[email protected] sbin]# if [ -d /home/ ]; then echo ok; fi
ok
[[email protected] sbin]# if [ -f /home/ ]; then echo ok; fi

因为 /home/ 为目录为非文件,所以并不会显示 “ok” .

[[email protected] sbin]# if [ -f /root/test.txt ]; then echo ok; fi
ok
[[email protected] sbin]# if [ -r /root/test.txt ]; then echo ok; fi
ok
[[email protected] sbin]# if [ -w /root/test.txt ]; then echo ok; fi
ok
[[email protected] sbin]# if [ -x /root/test.txt ]; then echo ok; fi
[[email protected] sbin]# if [ -e /root/test1.txt ]; then echo ok; fi

在shell 脚本中,除了用if来判断逻辑外,还有一种常用的方式,那就是case了。具体格式为:

case  变量  in
value1)
          command
          ;;
value2)
          command
          ;;
value3)
          command
          ;;
*)
          command
          ;;
esac

上面的结构中,不限制value的个数, * 则代表除了上面的value外的其他值。下面阿铭写一个判断输入数值是奇数或者偶数的脚本:

[[email protected] sbin]# cat case.sh
#! /bin/bash

read -p "Input a number: " n
a=$[$n%2]
case $a in

    1)
        echo "The number is odd."
        ;;
    0)
        echo "The number is even."
        ;;
    *)
        echo "It‘s not a number!"
        ;;
esac

$a 的值或为1或为0,执行结果为:

[[email protected]st sbin]# sh case.sh
Input a number: 100
The number is even.
[[email protected] sbin]# sh case.sh
Input a number: 101
The number is odd.

case脚本常用于编写系统服务的启动脚本,例如/etc/init.d/iptables中就用到了,你不妨去查看一下。

时间: 2024-12-19 18:40:51

shell脚本中的逻辑判断的相关文章

20.5 Shell脚本中的逻辑判断;20.6 文件目录属性判断;20.7 if特殊用法;20.8 20.9 cace判断(上下)

扩展: select用法 http://www.apelearn.com/bbs/thread-7950-1-1.html 20.5 Shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 1. 创建if1.sh测试脚本: [[email protected] ~]# vi if1.sh a=5,如果a大于3,满足这个条件,显示ok 添加内容: #!/bin/bash a=5 if [ $a -gt 3 ] then echo ok fi 2. 执行if1.sh脚本: [[e

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

笔记内容: 20.5 shell脚本中的逻辑判断 20.6 文件目录属性判断 20.7 if特殊用法 20.8/20.9 case判断 笔记日期:2017-11-22 20.5 shell脚本中的逻辑判断 在所有的编程语言中都会有if语句来进行逻辑判断,所以在shell中也不例外. Shell的if语句的判断条件和其他编程语言一样写在if关键字的那一行,但是需要使用方括号括起来,并且变量和逻辑运算符以及方括号都要用空格隔开,这一点和其他的编程语言不一样,整个if语句块以fi关键字表示结尾,the

20.5 shell脚本中的逻辑判断 20.6 文件目录属性判断 20.7 if特殊用法 20.8/20.9 case判断

- 20.5 shell脚本中的逻辑判断 - 20.6 文件目录属性判断 - 20.7 if特殊用法 - 20.8/20.9 case判断 # 20.5 Shell脚本中的逻辑判断 - 很多脚本可以直接用命令执行,比如之前的那个 ``` [[email protected] ~]# for i in `seq 1 5`;do echo $i;done 1 2 3 4 5 [[email protected] ~]# for i in `seq 1 5` > do > echo $i > 

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

一.shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 格式2:if 条件; then 语句; else 语句; fi 格式3:if -; then - ;elif -; then -; else -; fi if:如果. then:然后. -gt:大于. -lt:小于. -eq:等于. -ne:不等于.noeq. -ge:大于等于. -le:小于等于. 格式1:如果满足条件a大于3,则输出ok.最常用. # if [ $a -gt 3 ]; then echo ok;

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

一.shell脚本中的逻辑判断在shell脚本中,很多都会逻辑判断,判断某一个数值,判断某一个文件,或者某一个目录,我们针对判断结果再做一些操作,如果没有判断,就没法做一些操作格式1:if条件:then语句:fi例子:[[email protected] ~]# if [ $a -ge 3 ] //分行写就是这样写 thenecho okfiok[[email protected] ~]# if [ $a -ge 3 ]; then echo ok; fi //这是一行写的格式//解释:-gt表

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

一.shell脚本中的逻辑判断 语法1.格式1if 条件 ; then 语句; fi例:如果a大于3,打印OK [[email protected] ~]# vi 2.sh [[email protected] ~]# bash -v 2.sh #!/bin/bash a=5 if [ $a -gt 3 ] then echo ok fi ok [[email protected] ~]# bash 2.sh ok [[email protected] ~]# 2.格式2if 条件; then

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

1.Shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi格式2:if 条件; then 语句; else 语句; fi格式3:if -; then - ;elif -; then -; else -; fi逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格可以使用 &

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

shell脚本中的逻辑判断 shell脚本中很多都是逻辑判断,判断某个数值,判断某一个文件,或者某个目录,最后真对判断的某个结果再去做一些操作,在shll脚本中到处都是逻辑判断,如果没有判断就没有办法去做一些操作,不然只能敲命令了, shell中逻辑判断的语法 最常见的语法 :if 条件 ; then 语句; fi 他的格式比较特殊if和fi是相对的,正好对应的,then是if怎么样然后怎么样,这个语法读起来是如果怎么样然后怎么样 如果a的值大于3软后怎么样 [[email protected]

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

一.shell中的逻辑判断 格式1:if 条件 ; then 语句; fi a=2 if [ $a -ge 2 ]; then echo ">=2"; fi 格式2:if 条件; then 语句; else 语句; fi a=1 if [ $a -ge 2 ]; then echo ">=2"; else echo "<2"; fi 格式3:if -; then - ;elif -; then -; else -; fi a=6