一、bash编程入门
shell脚本:shebang
#!/bin/bash #解释此脚本的shell路径,内核调用对应的解释器来解释脚本
脚本中常交待的信息:
#Description:
#Version:
#Author:
#License:
#Datetime:
bash脚本:
文本文件,其中以#开头的行均为注释行,将会被解释器忽略
运行脚本内核会启动一个专门的shell进程来运行程序文件,脚本运行结束,此shell进程也即终止
运行脚本:
1、给脚本执行权限,而后指定路径来运行
内核会通过读取脚本文件第一行来判断启动什么解释器来运行此脚本
2、指定给释器来运行
bash命令常用的选项:
-n 注意:不是命令错误,而是检查语法错误
-x 调试运行 显示运行过程
[root@xxj shell]# cat 15.sh #!/bin/bash # sum1=1 sum2=2 sum=$[$sum1+$sum2] echo "The sum is $sum" [root@xxj shell]# bash -n 15.sh [root@xxj shell]# bash -x 15.sh + sum1=1 + sum2=2 + sum=3 + echo 'The sum is 3' The sum is 3
自定义脚本的状态结果:
exit[n]
注意:脚本中无论哪个位置遇到exit命令就停止
[root@xxj shell]# cat 15.sh #!/bin/bash # sum1=1 sum2=2 sum=$[$sum1+$sum2] echo "hell worl" exit 55 echo "The sum is $sum" [root@xxj shell]# bash 15.sh hell world [root@xxj shell]# echo $? 55
二、条件语句:if
什么是条件语句?
是指一种表示假设的主从复合句,一般由连词 if 引导的条件从句引出某种假设,再由主句表示基于这种假设下的反馈. “如果[或假如]……那么……” “当.....那么...” 选择执行
语法:
单分支if语句
if CONDITION
then if-true-分支
fi
当CONDITION成立时执行if-true-分支,否则退出
[root@BAIYU_207 shell]# bash -x 1.sh + '[' -d /etc/sysconfig/network-scripts ']' + echo exist exist [root@BAIYU_207 shell]# cat 1.sh #!/bin/bash if [ -d /etc/sysconfig/network-scripts ] then echo "exist" fi
双分支if语句:
if CONDITION
then if-true-分支
...
else if-false-分支
fi
当CONDITION成立时执行if-true-分支,否则执行if-false-分支
[root@BAIYU_207 shell]# bash -x 1.sh + '[' -f /etc/sysconfig/network-scripts ']' + echo 'not exist' not exist [root@BAIYU_207 shell]# cat 1.sh #!/bin/bash if [ -f /etc/sysconfig/network-scripts ] then echo "exist" else echo "not exist" fi
多分支if语句:
if CONNITION1
then if-true-分支1
elif CONNITION2
then if-true-分支2
elif CONNITION3
then if-true-分支3
else if-false-分支
fi
当CONNITION1成立时执行if-true-分支1,当CONNITION3成立时执行if-true-分支3,当所有条件都不成立则执行if-false-分支
[root@BAIYU_207 shell]# bash -x 1.sh + '[' -f /etc/sysconfig/network-scripts ']' + '[' '!' -c /dev/zero ']' + '[' -n /dev/null ']' + echo cccccccccc cccccccccc [root@BAIYU_207 shell]# cat 1.sh #!/bin/bash if [ -f /etc/sysconfig/network-scripts ] then echo "aaaaaaaaaa" elif [ ! -c /dev/zero ] then echo "bbbbbbbbbbbbb" elif [ -n /dev/null ] then echo "cccccccccc" else echo "not exist" fi
三、read命令
read 是从输入文件或者标准输入中或cat等输出的信息中,每次读取一行进行处理。
read命令使用格式:
read [OPTIONS] VAR...
-p:输出一句话来当作提示
-t:等待多少秒后退出
-n:当接收的字符达到预定数目时,自动退出,并将输入的值赋值给变量
-s:隐藏输入数据,适用于机密信息的输入
[root@BAIYU_207 shell]# read -p "please enter yourname: " name please enter yourname: xxj [root@BAIYU_207 shell]# echo $name xxj [root@BAIYU_207 shell]# read -t 5 -p "please enter yourname: " name please enter yourname: [root@BAIYU_207 shell]# [root@BAIYU_207 shell]# echo $name [root@BAIYU_207 shell]# read -n 2 -p "please enter yourname: " name please enter yourname: xx[root@BAIYU_207 shell]# echo $name xx [root@BAIYU_207 shell]# read -s -p "please enter yourname: " name please enter yourname: [root@BAIYU_207 shell]# echo $name xxj
在read命令行中也可以不指定变量.如果不指定变量,那么read命令会将接收到的数据放置在环境变量REPLY中。
环境变量REPLY中包含输入的所有数据,可以像使用其他变量一样在shell脚本中使用环境变量REPLY.
[root@BAIYU_207 shell]# read -t 5 -p "please enter yourname: " please enter yourname: xxj [root@BAIYU_207 shell]# echo $REPLY xxj
在上面read后面的变量只有name一个,也可以有多个,这时如果输入多个数据,则第一个数据给第一个变量,第二个数据给第二个变量,如果输入数据个数过多,则最后所有的值都给最后一个变量。如果输入不够则后面的变量为空
[root@BAIYU_207 shell]# read -p "please enter yourname: " name1 name2 name3 please enter yourname: aa bb cc dd ee [root@BAIYU_207 shell]# echo $name1 aa [root@BAIYU_207 shell]# echo $name2 bb [root@BAIYU_207 shell]# echo $name3 cc dd ee
练习:
1、写一个脚本,判断用户输入的是什么类型的文件
[root@BAIYU_207 shell]# cat 2.sh #!/bin/bash # read -t 10 -p "Please input a filename: " FILE if [ -z $FILE ] then echo "Error please input a filename" exit 1 elif [ ! -e $FILE ] then echo "you input is not a file" exit 2 elif [ -f $FILE ] then echo "you input is a general file" elif [ -d $FILE ] then echo "you input is a directory" else echo "you input is an other filesystem" fi
2、写一个脚本可接受以下四个参数并执行相应的操作
start:创建文件/var/lock/subsys/SCRIPT_NAME,
stop:删除此文件
restart:删除此文件并重新创建此文件,
status:如果此文件存在,显示为running,否则显示为“stop”
#!/bin/bash # prog=$(basename $0) lockfile="/var/lock/subsys/$prog" if [ "$1" == "start" ];then 测试发现这里$1不加双引号的话,如果$1的值为空就会报错,所以已经要注意都加上 if [ -e $lockfile ];then echo "The $prog is started yet." else touch $lockfile && echo "The $prog start success..." || echo "The $prog start failed" fi elif [ "$1" == "stop" ];then if [ -e $lockfile ];then rm -f $lockfile && echo "The $prog stop success..." else echo "The $prog stopped yet" fi elif [ "$1" == "restart" ];then if [ -e $lockfile ];then rm -f $lockfile && echo "The $prog stop success..." && touch $lockfile && echo "The $prog start success" else touch $lockfile && echo "The $prog stop failed" && touch $lockfile && echo "The $prog start success" fi elif [ "$"1 == "status" ];then if [ -e $lockfile ];then echo "The $prog is running" else echo "The $prog stopped" fi else echo "Usage:start|stop|restart|status" exit 1 fi # 测试结果 [root@BAIYU_110 shell]# bash 2.sh Usage:start|stop|restart|status [root@BAIYU_110 shell]# bash 2.sh aa bb Usage:start|stop|restart|status [root@BAIYU_110 shell]# bash 2.sh start The 2.sh is started yet. [root@BAIYU_110 shell]# bash 2.sh stop The 2.sh stop success... [root@BAIYU_110 shell]# bash 2.sh start The 2.sh start success... [root@BAIYU_110 shell]# bash 2.sh restart The 2.sh stop success... The 2.sh start success [root@BAIYU_110 shell]# bash 2.sh stop The 2.sh stop success... [root@BAIYU_110 shell]# bash 2.sh stop The 2.sh stopped yet [root@BAIYU_110 shell]# bash 2.sh restart The 2.sh stop failed The 2.sh start success [root@BAIYU_110 shell]#
四、条件语句:case
简洁版多分支if语句
使用场景:判断某变量的值是否为多种情形中的一种时使用
语法:
case $VARIABLE in
PATTERN1)
分支1
;;
PATTERN2)
分支2
;;
...
*)
分支n
;;
esac
PATTERN可使用glob模式的通配符:
*: 任意长度的任意字符;
?: 任意单个字符;
[]: 指定范围内的任意单个字符;
a|b: 多选1;
练习:
3、提示键入任意一个字符;判断其类型
[root@BAIYU_110 shell]# cat 3.sh #!/bin/bash # read -p "Plz enter a character: " char case $char in [a-z]) echo "A character." ;; [0-9]) echo "A digit." ;; *) echo "A special character." ;; esac # 测试结果: [root@BAIYU_110 shell]# vi 3.sh [root@BAIYU_110 shell]# bash 3.sh Plz enter a character: 3333 A special character. [root@BAIYU_110 shell]# bash 3.sh Plz enter a character: 4 A digit. [root@BAIYU_110 shell]# bash 3.sh Plz enter a character: 44 A special character. [root@BAIYU_110 shell]# bash 3.sh Plz enter a character: abc A special character. [root@BAIYU_110 shell]# bash 3.sh Plz enter a character: a A character. [root@BAIYU_110 shell]# bash 3.sh Plz enter a character: 4 a A special character. [root@BAIYU_110 shell]# bash 3.sh Plz enter a character: 4a A special character.
4、用case语句改写练习2的脚本
#!/bin/bash # prog=$(basename $0) lockfile="/var/lock/subsys/$prog" case $1 in start) if [ -e $lockfile ];then echo "The $prog is started yet." else touch $lockfile && echo "The $prog start success..." || echo "The $prog start failed" fi ;; stop) if [ -e $lockfile ];then rm -f $lockfile && echo "The $prog stop success..." else echo "The $prog stopped yet" fi ;; restart) if [ -e $lockfile ];then rm -f $lockfile && echo "The $prog stop success..." && touch $lockfile && echo "The $prog start success" else touch $lockfile && echo "The $prog stop failed" && touch $lockfile && echo "The $prog start success" fi ;; status) if [ -e $lockfile ];then echo "The $prog is running" else echo "The $prog stopped" fi ;; *) echo "Usage:start|stop|restart|status" exit 1 esac # 测试结果 [root@BAIYU_110 shell]# bash 4.sh Usage:start|stop|restart|status [root@BAIYU_110 shell]# bash 4.sh sb Usage:start|stop|restart|status [root@BAIYU_110 shell]# bash 4.sh status The 4.sh stopped [root@BAIYU_110 shell]# bash 4.sh stop The 4.sh stopped yet [root@BAIYU_110 shell]# bash 4.sh restart The 4.sh stop failed The 4.sh start success [root@BAIYU_110 shell]# bash 4.sh status The 4.sh is running [root@BAIYU_110 shell]# bash 4.sh start The 4.sh is started yet. [root@BAIYU_110 shell]# bash 4.sh stop The 4.sh stop success... [root@BAIYU_110 shell]# bash 4.sh status The 4.sh stopped