bash脚本的书写规范简介shell
看本文须要了解的脚本撰写习惯:bashcentos
开头顶格写#!紧接着写解释器路径/bin/bashbash
因为bash属于脚本语言,脚本语言的运行方式socket
解释运行:源代码 --> 运行时启动解释器,由解释器边解释边运行ide
Linux中的脚本解释器有:zsh,csh,bash,tsh众多shell,不过bash最经常使用。测试
第一行写完以后,就能够直接写代码。不过为了便于他人阅读一般会增长以下行:
centos7
第二行:#版本信息spa
第三行:#做者与联系方式ip
第四行:#版权宣告方式it
第五行:#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多分支,在等值比较状况下使用效果较好,简化代码量。
选择语句的做用:
在脚本执行时按触发的条件进入相应的语句执行。
何时使用选择语句?
一般就是须要对一些问题分状况讨论时用,这个须要在写的过程当中细细体会。