1、IF 判断语句
#! /bin/bash
#[] 该符号是test简化书写,test可以用来判断文件目录,数值大小
if [ $usr=root ] ;then
echo "running successul "
else
echo "running fault"
fi
~
---------------------------------------------------------
if-then-else 语句可以在条件不成立时也运行相应的命令
if [测试条件成立] ;then
echo 执行相应命令
elif [ 测试条件成立 ] ;then
echo 执行相应命令
else
echo 测试条件不成立,执行相应命令
fi 结束
--------------------------------------------------------------
#! /bin/bash
# root user1 other
if [ $usr=root ] ;then
echo "The usr is root"
elif [ $usr=usr1 ] ;then
echo "The usr is usr1"
else
echo "The usr is other"
fi
-----------------------------------------------------
2、嵌套IF语句
嵌套if的使用
if条件测试中可以再嵌套if条件测试
if [测试条件成立]; then
echo
if [测试条件成立]; then
echo
fi
fi
--------------------------------------
#! /bin/bash
if [ $usr=usr3 ] ;then
echo "The usr is other"
if [ -x /home/harbo/test2.sh ] ;then #标红部分,用于判断文件是否具有执行权限
echo " The file have executive permission"
fi
else
echo " The usr is root"
fi
--------------------------------------
3、case语句和select语句
case语句和select语句可以构成分支
case "$变量" in
"情况1")
命令.... ;;
'"情况2")
命令.... ;;
"情况3"
命令... ;;
"情况4"
命令... ;;
esac
-------------------------------------------------------
#! /bin/bash
case "$1" in
"start"|"START")
echo $0 The cmd is start or START ;;
"stop"|"STOP")
echo $0 The cmd is stop or STOP ;;
"reload"|"restart")
echo $0 The cmd is reload or restart ;;
*)
echo "usage: $0 {start|reload|stop|restart}" ;;
esac
如何理解$0,该命令代表的是第一个命令,
$1,该命令代表的是第二个命令,
example:harbo@harbo-virtual-machine:~$ bash script.sh restart
---------------------------------
4、for循环语句
使用for循环,for是逐个执行或者操作的这个一个语法。
1、for循环的语法
for 参数 in 列表
do
执行的命令
done 封闭一个循环
2、使用反引号或$()方式执行命令,命令的结果当作列表进行处理
3、练习
3.1 使用列表方式进行
--------------------------------------
for i in {1..9}
do
echo $i
done
----------------------------------------
效果展示
harbo@harbo-virtual-machine:~$ for i in {1..9}
> do
> echo $i
> done
1
2
3
4
5
6
7
8
9
--------------------------------------------
3.2 使用命令等方式批量对MP3进行改名
for filename in `ls *.mp3`
do
mv $filename $(basename $filename .mp3).mp4
done
---------------------------------------------------------
效果展示
harbo@harbo-virtual-machine:~$ for filename in `ls *.mp3`
> do
> mv $filename $(basename $filename .mp3).mp4
> done
harbo@harbo-virtual-machine:~$ ls
a.mp4 Desktop minikube Pictures script.sh test1.sh Videos
b.mp4 Documents minikube-linux-amd64 pktgen.sh snap test2.sh wordpress.sh
c.mp4 Downloads Music
5、C语言的for循环
C语言风格的for循环
shell,其实不擅长加减乘除的运算,但有时候需要测试请求的话,可以使用
for (( i=1 ; i<=10 ; i++))
do
echo $1
done
6、while and until 语句
while test测试一直为真的话,它将一直执行下去,两者都比较吃资源,不建议生产环境使用
----------------------------------------------
i=1
while [$i -le 10]
do
((i++))
echo $i
done
-----------------------------------------------
演示
harbo@harbo-virtual-machine:~$ i=1
harbo@harbo-virtual-machine:~$ while [$i -le 10]
> do
> ((i++))
> echo $i
> done
-----------------------------------------------
如何构建死循环,无论什么都是真,直到输入exit才退出
while :
do
echo always
done
------------------------------------------------------
until test测试一直为假的话,它将一直执行下去
----------------------------------------------
i=1
while [$i -gt 10]
do
((i++))
echo $i
done
7、for循环嵌套if语句
-------------------------------------------------
for循环,嵌套if语句
for script_name in /etc/profile.d/*.sh
do
if [-x $script_name ]; then
.$script_name
fi
done
--------------------------------------------------
for循环嵌套break 语句,(当变量num等于5的时候,就运行终止,就执行for循环的echo结束)
for num in {1..9}
do
if [ $num -eq 5 ]; then
break
fi
echo $num
done
----------------------------------------------------
for循环嵌套continue语句(当变量num等于5的时候,就跳过该数值,继续往后面for循环,输出echo结束)
for num in {1..9}
do
if [ $num -eq 5 ]; then
continue
fi
echo $num
done
8、for循环处理命令行参数
使用循环处理命令行参数
1.命令行参数可以使用$1 $2...$(10)...$n进行读取
2.$0 代表脚本名称
3.$*和$@代表所有位置参数
4.$#代表位置参数的数量
example: ./test.sh a b c d , 标红色部分就是代表$0, $1就是代表a,$2就是代表b
#! /bin/bash
#针对位置参数进行显示
for pos in $*
do
if [ "$pos" = "a" ];then
echo $pos $pos
fi
done
-----------------------------------------------------------
while [ $# -ge 1 ]
do
if [ "$1" = "a" ]; then
echo $1 $1
fi
shift
done
9、自定义函数
自定义函数
1. 函数用于 "包含“ 重复使用的命令集合
2. 自定义函数
function fname(){
命令
}
函数的执行
fname
------------------------------------
简单1点:定义一个cd
function cdls () {
cd
ls
}
定义一个checkpid 的函数
function checkpid () {
local i #本地环境生效
for i in $*
do
[ -d "/proc/$i" ] && return 0
done
return 1
}
~
-----------------------------
chmod u+x checkpid.sh
source checkpid.sh
checkpid 1
echo $?
10、系统自建函数
1、系统自建了函数库,可以在脚本中引用
/etc/init.d/functions
2、自建函数库
使用source 函数脚本文件”导入函数“
3、/etc/profile 系统环境变量
4、./bashrc 家目录
5、./bash_profile 家目录
12、信号捕获
捕获信号好笨的编写
1.kill 默认会发送15号信号给应用程序
2.ctrl+c 发送2号信号给应用程序
3.9号信号不可阻塞
-------------------
# signal demo,while 容易消耗大量的CPU资源,所以不建议长时间运行
trap ''echo signal 15" 15
trap ''echo signal 2" 2
echo $$
while :
do
:
done
当需要杀死脚本pid时,需要执行
kill -9 pid
13、一次性计划
at 由于它没有终端显示,所以一般都需要借助重定向
例如6:31分,需要将hello 写入到/tmp/hello.txt中
at 18:31
echo hello > /tmp/hello.txt
ctrl+d 结束
查看未执行的任务
atq
------------------------------------------------------------
示例演示
harbo@harbo-virtual-machine:~$ at 18:30
warning: commands will be executed using /bin/sh
at Tue Jul 12 18:30:00 2022
at> echo hello > /tmp/hello.txt
at> <EOT>
job 1 at Tue Jul 12 18:30:00 2022
harbo@harbo-virtual-machine:~$ atq
1 Tue Jul 12 18:30:00 2022 a harbo
harbo@harbo-virtual-machine:~$
15、周期性计划
1、Crontab
配置方式
crontab -e
查看现有的计划任务
crontab -l
配置格式:
分钟 小时 日期 月份 执行的命令
注意命令的路径问题 例如 data 可以使用which date 查询路径
harbo@harbo-virtual-machine:~$ which date
/usr/bin/date
0 2 14 * * /usr/bin/date >/tmp/date.txt
每个月的14号2点整执行该命令
每个用户都有自己crontab的目录
harbo@harbo-virtual-machine:~$ sudo ls /var/spool/cron/crontabs/harbo
/var/spool/cron/crontabs/harbo
16、给执行文件上锁,防止执行文件多次执行导致程序中止
给执行文件上一把锁,也就是让执行文件执行一次,防止后续重复执行导致文件终止
flock [options] <file|directory> -c <command>
flock -xn “/tmp/f.loc ” -c ''/root/a.sh"
-x 是独占一把锁
-n 失败而不是等待
-u 移除锁
flock -un /tmp/test1.sh -c /tmp/test1.sh