Shell脚本(脚本中的逻辑判断,逻辑判断表达式,判断文件和目录属性,case判断)

逻辑判断表达式:

-gt = 大于    -lt = 小于    -ge = 大于等于   -le = 小于等于   -eq = 恒等于  -ne = 不等于

这里要注意空格

也可以用&&和||结合多个条件

例如1:

[[email protected] shell]# cat if4.sh

#/bin/bash

a=5

if [ $a -gt 4 ] && [ $a -lt 6 ]

then

echo "4<a<6"

else

echo nook

fi

[[email protected] shell]# sh if4.sh

4<a<6

一、Shell中的逻辑判断

格式1:if条件;then语句;fi        (如果...然后...)

例如:

[[email protected] shell]# cat if1.sh

#/bin/bash

a=3

if [ $a -gt 1 ]

then

echo ok

fi

结果:

[[email protected] shell]# sh if1.sh

ok

格式2:if条件;then语句;else语句;fi        (如果...然后...;不满足条件...然后....)

示列:

[[email protected] shell]# cat if2.sh

#/bin/bash

a=1

if [ $a -gt 3 ]

then

echo ok

else

echo no ok

fi

结果:

[[email protected] shell]# sh if2.sh

nook

格式3:if...;then...;elif...;then...;else...;fi (如果...然后...;还有...然后...,不满足条件...然后...)

(可以添加多个elif)

例如:

[[email protected] shell]# cat if3.sh

#/bin/bash

a=*

if [ $a -gt 6 ]

then

echo "a > 3"

elif

[ $a -lt 9 ]

then

echo "< 9"

else

echo no ok

fi

当a等于5时

[[email protected] shell]# sh if3.sh

< 9

当a等于7时

[[email protected] shell]# sh if3.sh

a > 3

所以当if满足条件,elif不会执行

二、逻辑判断表达式:

-gt = 大于    -lt = 小于    -ge = 大于等于   -le = 小于等于   -eq = 恒等于  -ne = 不等于

这里要注意空格

也可以用&&和||结合多个条件

例如1:

[[email protected] shell]# cat if4.sh

#/bin/bash

a=5

if [ $a -gt 4 ] && [ $a -lt 6 ]

then

echo "4<a<6"

else

echo nook

fi

[[email protected] shell]# sh if4.sh

4<a<6

Shell中的逻辑判断

格式1:if条件;then语句;fi        (如果...然后...)

例如:

[[email protected] shell]# cat if1.sh

#/bin/bash

a=3

if [ $a -gt 1 ]

then

echo ok

fi

结果:

[[email protected] shell]# sh if1.sh

ok

格式2:if条件;then语句;else语句;fi        (如果...然后...;不满足条件...然后....)

示列:

[[email protected] shell]# cat if2.sh

#/bin/bash

a=1

if [ $a -gt 3 ]

then

echo ok

else

echo no ok

fi

结果:

[[email protected] shell]# sh if2.sh

nook

格式3:if...;then...;elif...;then...;else...;fi (如果...然后...;还有...然后...,不满足条件...然后...)

(可以添加多个elif)

例如:

[[email protected] shell]# cat if3.sh

#/bin/bash

a=*

if [ $a -gt 6 ]

then

echo "a > 3"

elif

[ $a -lt 9 ]

then

echo "< 9"

else

echo no ok

fi

当a等于5时

[[email protected] shell]# sh if3.sh

< 9

当a等于7时

[[email protected] shell]# sh if3.sh

a > 3

所以当if满足条件,elif不会执行

三、判断文件,目录属性

[-f file]判断是否是普通文件,是否存在

例如:

[[email protected] shell]# cat file1.sh

#/bin/bash

f="/tmp/renxinyren"

if [ -f $f ]

then

echo $f exist

else

touch $f

echo  "touch $f"

第一次执行:

[[email protected] shell]# sh file1.sh

touch /tmp/renxinyren

第二次执行:

[[email protected] shell]# sh file1.sh

/tmp/renxinyren exist

[-d file]判断是否是普通目录,是否存在

例如:

[[email protected] shell]# cat file2.sh

#/bin/bash

f="/tmp/renxinyren"

if [ -d $f ]

then

echo $f cunzai

else

mkdir $f

echo  "mkdir $f"

fi

[[email protected] shell]# sh file2.sh

mkdir /tmp/renxinyren

[[email protected] shell]# sh file2.sh

/tmp/renxinyren cunzai

[-e file]只是判断文件或者目录是否存在

[[email protected] shell]# cat file3.sh

#/bin/bash

f="/tmp/renxinren"

if [ -e $f ]

then

echo $f cunzai

else

echo $f bucunzai

fi

[[email protected] shell]# sh file3.sh

/tmp/renxinren bucunzai

[ -r $f ] [ -w $f ] [ -x $f ]分别表示辨别一个文件是否可读,可写,可执行

[[email protected] shell]# ll /tmp/rxr

-rw-r--r-- 1 root root 0 1月  11 15:16 /tmp/rxr

[[email protected] shell]# cat file4.sh

#/bin/bash

f="/tmp/rxr"

if [ -r $f ]

then

echo $f yes

else

echo $f no

fi

[[email protected] shell]# sh file4.sh

/tmp/rxr yes

修改为[-w $f]

[[email protected] shell]# vim file4.sh

[[email protected] shell]# sh file4.sh

/tmp/rxr yes

修改为[-x $f]

[[email protected] shell]# vim file4.sh

[[email protected] shell]# sh file4.sh

/tmp/rxr no

四、case判断

我们在介绍case这个命令之前,先解释一个read -p命令。

我们先用命令行来测试一下这个命令。

[[email protected] ~]# read -p "Please input a number: " n

Please input a number: 123

[[email protected] ~]# echo $n

123

通过上面的实验我们可以发现,read -p 可以和用户实现一个交互,并赋给变量一个值。

删除笔记修改笔记

我们等会写一个关于case的脚本。我们把它分成2部分来解析。首先看第一部分:

#/bin/bash

read -p "Please input a number: " n

if [ -z "$n" ]

then

echo "Please input a number."

exit 1

fi

首先,我们让用户输入一个数字,-z表示,如果变量为空,那么会返回一个Please input a number的字符串,并且exit退出,=。

还记得我们编译时,如果失败了,当我们用echo !$查询时,发现返回值为1,这里的1就是这个意思。

n1=`echo $n|sed 's/[0-9]//g'`

if [ -n "$n1" ]

then

echo "Please input a number."

exit 1

fi

如果用户输入的值不为空,那么跳入这一步,首先会把用户输入的变量拿出来,做一个过滤,sed命令把所有数字全部清空,-n表示如果sed清空数字后,字符不为空,则表示用户输入了一个不是数字的字符,则会返回,Please input a number!,这里加一个!和上面的做区分。

执行结果:

[[email protected] shell]# sh case1.sh

Please input a number:

Please input a number.

[[email protected] shell]# sh case1.sh

Please input a number: ds1

Please input a number!!

第二部分:使用了if...then...;elif...then...;eles....

如果变量是n大于等于0,小于等于60,则变量tag=1。

如果变量n大于等于60,小于等于80,则变量tag=2。

如果变量n大于等于80,小于等于90,则变量tag=3。

如果变量n大于等于90,小于等于100,则变量tag=4。

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循环了.注意格式,case 变量 in ,1)...;; 2)...;; 3)...;;,最后结尾用esac,我们也可以|来表示并列,例如,3|4),就是当变量等于3或者4时,返回下面的命令。

当变量tag=1时,返回not ok

当变量tag=2时,返回ok

当变量tag=3时,返回ook

当变量tag=4时,返回oook

当变量等于其他时,说明用户输入的值不在这个范围内,则返回值"The number range is 0-100."

case $tag in

1)

echo "not ok"

;;

2)

echo "ok"

;;

3)

echo "ook"

;;

4)

echo "oook"

;;

*)

echo "The number range is 0-100."

;;

esac

原文地址:http://blog.51cto.com/13407306/2069425

时间: 2024-08-25 22:39:33

Shell脚本(脚本中的逻辑判断,逻辑判断表达式,判断文件和目录属性,case判断)的相关文章

shell中的逻辑判断,if 判断文件、目录属性,if判断的一些特殊用法

shell中的逻辑判断 格式1:if 条件 ; then 语句; fi //如果满足条件,然后执行语句 [[email protected] shell]# cat if1.sh #!/bin/bash a=5 if [ $a -gt 3 ] then echo ok fi [[email protected] shell]# sh -x if1.sh + a=5 + '[' 5 -gt 3 ']' + echo ok ok 格式2:if 条件; then 语句; else 语句; fi //如

shell中判断文件或目录是否存在

可以通过man test来查看文档,下面的文章转自http://m.blog.csdn.net/blog/yuanjungogogo/9222875 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" # 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限 if [ ! -x "$myPath"]; then mkdir "$myPa

Mac 中显示资源库(Library)文件夹目录的几种方法

Mac 中显示资源库(Library)文件夹目录的几种方法 Mac中Library目录在10.6.7系统之后默认隐藏的,要想找到此文件夹有如下几种方法: 1. 用命令可以使其显示: 在终端中执行命令: chflags nohidden ~/Library 可显示资源库文件夹 如想隐藏,可以在终端中执行命令: chflags hidden ~/Library 隐藏 2. 在Finder菜单中的偏好设置中设置 在Finder菜单中的偏好设置中选择边栏,勾选上设备中的硬盘. 再打开Finder,Fin

Linux rm(删除一个目录中的一个或多个文件或目录或删除非空目录)

rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除了链接,原有文件均保持不变. rm是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比如在/(根目录)下执行rm * -rf).所以,我们在执行rm之前最好先确认一下在哪个目录,到底要删除什么东西,操作时保持高度清醒的头脑. 命令格式: rm [选项] 文件- 命令功能: 删除一个目录中的一个或多个文件或目录,如

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

20.5shell脚本中的逻辑判断格式1:if 条件 ; then 语句; fi大部分时候都是用的这种格式格式2:if 条件; then 语句; else 语句; fi格式2:if 条件; then 语句; else 语句; fi20.6 文件目录属性判断[ -f file ]判断是否是普通文件,且存在执行过程上面不存在已创建,再次执行就已存在[ -d file ] 判断是否是目录,且存在-d查看是否是目录,不是就创建-e查看是否存在在shell里touch是可以创建目录与文件的,如果目录下已存

将window的shell脚本通过ftp传输到Linux服务器后, shell脚本中执行时提示“没有那个文件或目录”的解决办法

出现bad interpreter:No such file or directory的原因,是文件格式的问题.这个文件是在Windows下编写的.换行的方式与Unix不一样,但是在vim下面如果不Set一下又完全看不出来. 问题分析:1.将windows 下编写好的SHELL文件,传到linux下执行,提示出错.2.出错信息:bad interpreter: 没有那个文件或目录. 问题原因:因为操作系统是windows,在windows下编辑的脚本,所以有可能有不可见字符.脚本文件是DOS格式

shell 判断文件、目录是否存在

shell判断文件是否存在 1. shell判断文件,目录是否存在或者具有权限 2. #!/bin/sh 3. 4. myPath="/var/log/httpd/" 5. myFile="/var /log/httpd/access.log" 6. 7. # 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限 8. if [ ! -x "$myPath"]; then 9. mkdir "$myPath" 10

用IO流中的File类来创建文件及目录

题目如下: 设计一个命令窗口程序,要求按照如下的格式显示 例如:===利用命令窗口完成文件的操作===(1)创建目录 (2)创建文件 (3)删除目录 (4)删除文件 ===现在正在做创建目录的操作===目录名字:liu目录路径:F:\\Jinglin\\ZY\\创建成功,选1回到命令窗口界面,选2退出程序 ===现在正在做创建文件的操作===文件名字:liu文件路径:F:\\Jinglin\\ZY\\创建成功,选1回到命令窗口界面,选2退出程序 ===现在正在做删除目录的操作===目录名字:li

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 >