//创建并编辑example.sh脚本[root@centos ~]# vim example.sh
#!/bin/bash
#按位输出参数
echo "The current name of the script is:$0."
echo "There are a total of $# parameters,They are $*, respectively."
echo "The first parameter is $1."
echo "The fifth parameter is $5."//运行脚本[root@centos ~]# ./example.sh one two three four five six
The current name of the script is:./example.sh.
There are a total of 6 parameters,They are one two three four five six, respectively.
The first parameter is one.
The fifth parameter is five.
3、if条件测试语句
//创建并编辑host.sh脚本
#!/bin/bash
#pingIP判断是否存活,-c规定尝试的次数,-i规定每个数据包的发送间隔时间,-W规定等待超时时间
ping -c 3-i 0.2-W 3 $1&>/dev/null
if[ $?-eq 0]
then
echo "Host $1 is On-line."else
echo "Host $1 is Off-line."
fi
//运行脚本[root@centos ~]# ./host.sh 192.168.2.15
Host 192.168.2.15 is Off-line.[root@centos ~]# ./host.sh 192.168.2.2
Host 192.168.2.2 is On-line.
//创建并编辑host.sh脚本[root@centos ~]# vim score.sh
#!/bin/bash
#判断用户输入的分数在那个成绩区间,超出提示不在范围内,read是用来读取用户输入的信息,-p是输出提示信息
read -p "Enter your score (0-100):" Grade
if[ $Grade -ge 85]&&[ $Grade -le 100]
then
echo "$Grade is Excellent!"
elif [ $Grade -ge 75]&&[ $Grade -lt 85]
then
echo "$Grade is Good!"
elif [ $Grade -ge 60]&&[ $Grade -lt 75]
then
echo "$Grade is qualified!"
elif [ $Grade -ge 0]&&[ $Grade -lt 60]
then
echo "$Grade is unqualified!"else
echo "$Grade is not within the range!"
fi
//运行脚本[root@centos ~]# ./score.sh
Enter your score(0-100):9090 is Excellent![root@centos ~]# ./score.sh
Enter your score(0-100):8080 is Good![root@centos ~]# ./score.sh
Enter your score(0-100):7070 is qualified![root@centos ~]# ./score.sh
Enter your score(0-100):5050 is unqualified![root@centos ~]# ./score.sh
Enter your score(0-100):200200 is not within the range!
4、for条件循环语句
//创建IP地址簿[root@centos 脚本]# vim ipaddress.txt
192.168.2.1192.168.2.2192.168.2.3192.168.2.4192.168.2.5192.168.2.6192.168.2.7192.168.2.8192.168.2.9192.168.2.10//创建并编辑检测IP地址簿的CheckHost脚本[root@centos 脚本]# vim CheckHost.sh
#!/bin/bash
hostlist=$(cat ~/脚本/ipaddress.txt)for IP in $hostlist
do
ping -c 3-i 0.2-W 3 $IP &>/dev/null
if[ $?-eq 0]
then
echo "Host $IP is Online."else
echo "Host $IP is Offline."
fi
done
//运行脚本[root@centos 脚本]# ./CheckHost.sh
Host 192.168.2.1 is Online.
Host 192.168.2.2 is Offline.
Host 192.168.2.3 is Offline.
Host 192.168.2.4 is Offline.
Host 192.168.2.5 is Offline.
Host 192.168.2.6 is Offline.
Host 192.168.2.7 is Offline.
Host 192.168.2.8 is Offline.
Host 192.168.2.9 is Offline.
Host 192.168.2.10 is Offline.
5、while条件循环语句
///创建并编辑猜价格的脚本[centos@centos 脚本]$ vim 猜价格.sh
#!/bin/bash
price=$(expr $RANDOM %1000)
time=0
echo "商品实际价格为0-999之间,猜猜看是多少?"while true
do
read -p "请输入您猜测的价格:" INT
let time++if[ $INT -eq $price ];
then
echo "恭喜您答对了!实际价格是:$price"
echo "您总共猜测了$time次!"
exit
elif [ $INT -ge $price ];
then
echo "太高了!"else
echo "太低了!"
fi
done
//运行脚本[centos@centos 脚本]$ ./猜价格.sh
商品实际价格为0-999之间,猜猜看是多少?
请输入您猜测的价格:100
太低了!
请输入您猜测的价格:999
太高了!
请输入您猜测的价格:500
太低了!
请输入您猜测的价格:600
太低了!
请输入您猜测的价格:800
太高了!
请输入您猜测的价格:700
太低了!
请输入您猜测的价格:750
太高了!
请输入您猜测的价格:725
太高了!
请输入您猜测的价格:720
太低了!
请输入您猜测的价格:723
太高了!
请输入您猜测的价格:721
恭喜您答对了!实际价格是:721
您总共猜测了11次!
6、case条件测试语句
//创建并编判断输入内容类型的脚本[centos@centos 脚本]$ vim 判断类型.sh
#!/bin/bash
read -p "Please enter a character and press Enter to confirm:" key
case"$key" in
[a-z]|[A-Z])
echo "You entered a letter.";;[0-9])
echo "You entered a number.";;*)
echo "You enter spaces, function keys, or other control character."
esac
//运行脚本[centos@centos 脚本]$ ./判断类型.sh
Please enter a character and press Enter to confirm:6
You entered a number.[centos@centos 脚本]$ ./判断类型.sh
Please enter a character and press Enter to confirm:z
You entered a letter.[centos@centos 脚本]$ ./判断类型.sh
Please enter a character and press Enter to confirm:
You enter spaces, function keys, or other control character.
三、计划任务服务程序
1、at命令的参数及作用
参数
作用
-f
指定包含命令的任务文件
-q
指定新任务名称
-l
显示待执行任务列表
-d
删除指定待执行任务
-m
任务执行后给用户发邮件
2、将系统设置为在今晚23:00自动重启网站服务
//采用交互式方法[centos@centos ~]$ at 23:00
warning: commands will be executed using /bin/sh
at> systemctl restart httpd
at><EOT>
job 1 at Thu Apr 2023:00:002023//采用非交互式方法[centos@centos ~]$ echo "systemctl restart httpd"| at 23:00
warning: commands will be executed using /bin/sh
job 2 at Thu Apr 2023:00:002023//显示待执行的任务列表[centos@centos ~]$ at -l
1 Thu Apr 2023:00:002023 a centos
3、crontab命令的参数及作用
参数
作用
-e
编辑计划任务
-u
指定用户名称
-l
列出人物列表
-r
删除计划任务
4、参数格式
如果有些字段没有被设置,需要使用型号(*)占位。
分钟 小时 日期 月份 星期 命令
50321* run command
5、假设在每周一、三、五的凌晨三点二十五分,将某个网站的数据目录打包处理,使其作为一个备份文件
//设置计划[centos@centos ~]$ crontab -e
no crontab for centos - using an empty one
crontab: installing new crontab
//查看待执行的任务列表[centos@centos ~]$ crontab -l
253**1,3,5/usr/bin/tar -czvf backup.tar.gz /home/wwwroot