没有合适的资源?快使用搜索试试~ 我知道了~
Linux-shell编程05Linux-shell编程05Linux-shell编程05Linux-shell编程05Linux-shell编程05Linux-shell编程05Linux-shell编程05Linux-shell编程05Linux-shell编程05Linux-shell编程05Linux-shell编程05Linux-shell编程05Linux-shell编程05Linux-shell编程05Linux-shell编程05
资源推荐
资源详情
资源评论
shell逻辑控制
先前我们的shell脚本都是按照顺序,⾃上⽽下的依次处理。但是许多程序要求对于shell脚本可以进⾏逻辑流程控
制,这类命令称之为 结构化命令(structured command)
结构化命令允许你修改程序执⾏的顺序。
!
if-then语句
语法
如果你之前学过其他编程语⾔,if语句后⾯要求语句的结果得是 True 或 False 。
但是bash的if语句并不然。
bash的if语句会直接运⾏if后⾯的命令,如果该命令执⾏正确(状态码为0),处于then的命令就会被执⾏。否则就
不会执⾏,或者执⾏其他逻辑的语句,最后到fi结束逻辑控制。
另⼀个案例
if command
then
command
fi
[root@web01 ~]# cat test.sh
#!/bin/bash
# testing the if statement
if pwd
then
echo "It worked"
fi
[root@web01 ~]#
[root@web01 ~]# bash test.sh
/root
It worked
[root@web01 ~]# cat test.sh
#!/bin/bash
# testing a bad command
if badcommand
then
echo "It worked."
fi
echo "We are outside the if statement."
[root@web01 ~]#
[root@web01 ~]#
www.apecome.com
猿来教育Linux云计算
这个案例就演示了if后⾯的命令⾮正确时,bash会跳过then的语句。
if-then-else语句
在这⾥,就增加了⼀个逻辑的选择
案例
可以看出语句翻译是
嵌套if
[root@web01 ~]# bash test.sh
test.sh: line 3: badcommand: command not found
We are outside the if statement.
if command
then
command
else
command
fi
[root@web01 ~]# cat test.sh
#!/bin/bash
# testing the else section
testuser=someone
if grep $testuser /etc/passwd
then
echo "The bash file for user $testuser are:"
ls -a /home/$testuser/.b*
echo
else
echo "The user $testuser does not exist on this system."
echo
fi
# 结果
[root@web01 ~]# bash test.sh
The user someone does not exist on this system.
如果 ..
那么
执⾏...
否则
执⾏...
结束
www.apecome.com
猿来教育Linux云计算
其实就是⽐刚才多⼏个判断,来看超哥怎么写
这种写法问题在于代码不易阅读,很难理清逻辑。
elif
语法
案例,这样的逻辑,更适合阅读。
1.创建⼀个测试的⽬录
[root@web01 ~]# mkdir /home/cccc
2.脚本
[root@web01 ~]# cat test.sh
#!/bin/bash
testuser=cccc
if grep $testuser /etc/passwd
then
echo "The user $testuser exists on this system."
else
echo "The user $testuser does not exist on this system."
if ls -d /home/$testuser/
then
echo "However,$testuser has a directory."
fi
fi
3.执⾏
[root@web01 ~]# bash test.sh
The user cccc does not exist on this system.
/home/cccc/
However,cccc has a directory.
if command1
then
commands
elif command2
then
more commands
fi
# 超哥翻译
如果 ...
那么
执⾏..
⼜如果...
那么
执⾏...
结束
www.apecome.com
猿来教育Linux云计算
再来⼀个案例,⽤于判断 cccc ⽤户的两种情况
有家⽬录,⽤户信息被删除
没有家⽬录,⽤户也不存在
[root@web01 ~]# cat test.sh
#!/bin/bash
testuser=cccc
if grep $testuser /etc/passwd
then
"The user $testuser exists on this system."
elif ls -d /home/$testuser
then
echo "The user $testuser does not exist on this system."
echo "However,$testuser has a directory."
fi
[root@web01 ~]# bash test.sh
/home/cccc
The user cccc does not exist on this system.
However,cccc has a directory.
[root@web01 ~]# cat test.sh
#!/bin/bash
testuser=cccc
if grep $testuser /etc/passwd
then
echo "The user $testuser exists on this system."
elif ls -d /home/$testuser
then
echo "The user $testuser does not exist on this system."
echo "However,$testuser has a directory."
else
echo "The user $testuser does not exist on this system."
echo "And $testuser does not have a directory."
fi
# 执⾏分别看结果
[root@web01 ~]# bash test.sh
/home/cccc
The user cccc does not exist on this system.
However,cccc has a directory.
# 删除家⽬录,再执⾏
[root@web01 ~]# rmdir /home/cccc/
[root@web01 ~]# bash test.sh
ls: cannot access /home/cccc: No such file or directory
The user cccc does not exist on this system.
And cccc does not have a directory.
www.apecome.com
猿来教育Linux云计算
test命令
test 命令最短的定义可能是评估⼀个表达式;如果条件为真,则返回⼀个 0 值。如果表达式不为真,则返回⼀个⼤于 0 的值
— 也可以将其称为假值。检查最后所执⾏命令的状态的最简便⽅法是使⽤ $? 值。
参数:
1. 关于某个⽂件名的『类型』侦测(存在与否),如 test -e filename
-e 该『⽂件名』是否存在?(常⽤)
-f 该『⽂件名』是否为⽂件(file)?(常⽤)
-d 该『⽂件名』是否为⽬录(directory)?(常⽤)
-b 该『⽂件名』是否为⼀个 block device 装置?
-c 该『⽂件名』是否为⼀个 character device 装置?
-S 该『⽂件名』是否为⼀个 Socket ⽂件?
-p 该『⽂件名』是否为⼀个 FIFO (pipe) ⽂件?
-L 该『⽂件名』是否为⼀个连结档?
2. 关于⽂件的权限侦测,如 test -r filename
-r 侦测该⽂件名是否具有『可读』的属性?
-w 侦测该⽂件名是否具有『可写』的属性?
-x 侦测该⽂件名是否具有『可执⾏』的属性?
-u 侦测该⽂件名是否具有『SUID』的属性?
-g 侦测该⽂件名是否具有『SGID』的属性?
-k 侦测该⽂件名是否具有『Sticky bit』的属性?
-s 侦测该⽂件名是否为『⾮空⽩⽂件』?
3. 两个⽂件之间的⽐较,如: test file1 -nt file2
-nt (newer than)判断 file1 是否⽐ file2 新
-ot (older than)判断 file1 是否⽐ file2 旧
-ef 判断 file2 与 file2 是否为同⼀⽂件,可⽤在判断 hard link 的判定上。 主要意义在判定,两个⽂件是否均指向同
⼀个 inode 哩!
4. 关于两个整数之间的判定,例如 test n1 -eq n2
-eq 两数值相等 (equal)
-ne 两数值不等 (not equal)
-gt n1 ⼤于 n2 (greater than)
-lt n1 ⼩于 n2 (less than)
-ge n1 ⼤于等于 n2 (greater than or equal)
-le n1 ⼩于等于 n2 (less than or equal)
5. 判定字符串的数据
test -z string 判定字符串是否为 0 ?若 string 为空字符串,则为 true
test -n string 判定字符串是否⾮为 0 ?若 string 为空字符串,则为 false。
注: -n 亦可省略
test str1 = str2 判定 str1 是否等于 str2 ,若相等,则回传 true
test str1 != str2 判定 str1 是否不等于 str2 ,若相等,则回传 false
www.apecome.com
猿来教育Linux云计算
剩余24页未读,继续阅读
资源评论
广西千灵通网络科技有限公司
- 粉丝: 4251
- 资源: 58
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功