您的位置:首页 > 运维架构 > Shell

Linux基础之bash脚本编程进阶篇-选择执行语句(if,case)

2016-09-05 14:34 781 查看
bash脚本的书写规范简介
看本文需要了解的脚本撰写习惯:bash
开头顶格写#!紧接着写解释器路径/bin/bash
由于bash属于脚本语言,脚本语言的运行方式
解释运行:源代码 --> 运行时启动解释器,由解释器边解释边运行
Linux中的脚本解释器有:zsh,csh,bash,tsh众多shell,不过bash最常用。
第一行写完之后,就可以直接写代码。不过为了便于他人阅读通常会增加如下行:

第二行:#版本信息
第三行:#作者与联系方式
第四行:#版权宣告方式
第五行:#History
一般会增加上面4行注释信息。
除第一行#外其余行只要#后内容脚本统统不识别,因此可用#作为注释使用
实验环境CentOS7.2

bash脚本语句执行顺序
bash脚本中分为顺序执行选择执行循环执行这大致三类执行顺序。
▲顺序执行,就是从上到下,从左到右的顺序逐一读取命令。
▲选择执行,根据一些条件的真伪情况分别执行相应的操作。
▲循环执行,根据限制条件及循环体,反复执行,直到不符合循环条件退出循环为止。

本文介绍较为简单的选择执行语句
选择执行语句

选择执行语句大致分为if语句case语句
………………………………………………………………………………………………………………………
if语句
if语句又分为if单分支语句if双分支语句if多分支语句
case可以认为是if多分支语句的一个特例,因为其格式更加精简所以在遇到这种特例会使用case。
………………………………………………………………………………………………………………………
if单分支语句
单分支:
if CONDITION; then
if-true
fi

CONDITION:可以是测试条件,可以是命令执行结果,如果CONDITION内容为真,则进入then阶段,之后进行相应操作,这个执行操作可以是命令,也可以是其他执行语句。也就是这里可以进行语句的嵌套
该操作结束后,使用fi进行if语句的结尾动作
………………………………………………………………………………………………………………………
下面举个简单例子:写一个脚本,判断1与2的大小,1<2则显示:2 is bigger
[root@localhost test]# cat >> if11 << EOF
> #!/bin/bash
> # This script is a value test for 1 and 2
> #2016-0828 author chawan
> #
> if [ 1 -lt 2 ];then
>     echo "2 is bigger"
> fi
> EOF
[root@localhost test]# chmod +x if11
[root@localhost test]# ./if11
2 is bigger
………………………………………………………………………………………………………………………
if双分支语句
双分支:
if CONDITION; then
if-true
else
if-false
fi
双分支语句跟单分支语句不同的地方在于else后面接的是CONDITION为假时的情况
同理else后面可以跟命令也可以跟语句嵌套。最后以fi结束if语句
………………………………………………………………………………………………………………………
示例:比较两个数的大小,如果第一个数大于等于第二个数则显示:First number is bigger,否则显示:Second number is bigger。
[root@localhost test]# cat if12
#!/bin/bash
# Test two number who is bigger
#2016-0828 author chawan
#
[ $# -ne 2 ] && echo "Please give two number !" && exit 1
if [ $1 -ge $2 ];then
echo "First number $1 is bigger"
else
echo "Second number $2 is bigger"
fi
[root@localhost test]# chmod +x if12
[root@localhost test]# ./if12
Please give two number !
[root@localhost test]# ./if12 3 9
Second number 9 is bigger
………………………………………………………………………………………………………………………
if多分支语句
多分支:
if CONDITION1; then
if-true
elif CONDITION2; then
if-ture
elif CONDITION3; then
if-ture
...
esle
all-false
fi
多分支语句跟双分支并没有什么大的区别,只是通过elif多了几个选择判断。只要理解了if双分支的使用,那么if的多分支就不成问题。
………………………………………………………………………………………………………………………
示例:输入一个文件的绝对路径判断该文件的类型。
#!/bin/bash
#version 1.0
#auther chawan
#date:20160905
#根据提示信息输入相应内容
read -p "Please give a path of file :" Path
#判断输入内容是否为空,为空则提示
test -z $Path && echo "No path" && exit 1
#判断输入的路径对应的文件是否存在,若不存在则提示路径错误并退出
! test -e $Path && echo "Wrong path " && exit 2
#多分支选择语句,判断是否为普通文件,目录文件及链接文件,其他类型表示为不识别。
if [ -f $Path ];then
echo "$Path is common file"
elif [ -d $Path ];then
echo "$Path is diretory file"
elif [ -h $Path ];then
echo "$Path is link file"
else
echo "Unknown file type"
fi
下面输入几个文件路径进行测试
[root@centos7 test]# sh if_3
Please give a path of file :/
/ is diretory file
[root@centos7 test]# sh if_3
Please give a path of file :/test/t1
/test/t1 is common file
[root@centos7 test]# sh if_3
Please give a path of file :
No path
[root@centos7 test]# sh if_3
Please give a path of file :/e
Wrong path
………………………………………………………………………………………………………………………
case语句
case语句:特点可以理解为特殊的if多分支语句
它在特定情况下使用会简化脚本。
case语句格式
case 变量引用 in
part1
分支1
;;
part2
分支2
;;
...
*)
默认分支
;;
esac
case中的变量引用内容需要等于in分支中的part#部分,也就是出现等值比较时使用case效果会很好。下面举一个例子。

………………………………………………………………………………………………………………………
示例:输入一个文件的绝对路径判断该文件的类型。
#!/bin/bash
#version 1.0
#auther chawan
#date:20160905
read -p "Please give a path of file :" Path
test -z $Path && echo "No path" && exit 1
! test -e $Path && echo "Wrong path " && exit 2
#取文件类型符:d,-,l,p,s,b,c
type_file=`ls -l -d $Path | cut -c1`
case $type_file in
-)
echo "$Path is common file"
;;
d)
echo "$Path is diretory file"
;;
l)
echo "$Path is link file"
;;
b)
echo "$Path is block file"
;;
c)
echo "$Path is char file"
;;
s)
echo "$Path is socket file"
;;
p)
echo "$Path is pipe file"
;;
*)
echo "Unknown file type"
;;
esac
下面输入几个文件路径进行测试
[root@centos7 test]# sh case_1
Please give a path of file :/etc/fstab
/etc/fstab is common file
[root@centos7 test]# sh case_1
Please give a path of file :/dev/sdb
/dev/sdb is block file
[root@centos7 test]# sh case_1
Please give a path of file :/usr
/usr is diretory file
[root@centos7 test]# sh case_1
Please give a path of file :/ee
Wrong path
[root@centos7 test]# sh case_1
Please give a path of file :
No path


小结

脚本中的一些注意事项:
脚本中不识别命令别名:以alias设定的别名在脚本中都不识别

选择语句总结:
if选择:if单分支,if双分支,if多分支
case选择:特殊的if多分支,在等值比较情况下使用效果较好,简化代码量

选择语句的作用:
在脚本执行时按触发的条件进入相应的语句执行。

什么时候使用选择语句?
通常就是需要对一些问题分情况讨论时用,这个需要在写的过程中细细体会。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  if;case