目录
1.for循环
for i in [取值列表] 可以取:数字 字符串 命令结果`` 序列
do
要执行什么命令
done
案列:测试1-255有多少个IP地址在线(能ping通则在线)10.0.0.1-255
[root@web01 /server/scripts]# cat pingv2.sh
#!/bin/sh
for i in {1..254}
do
{
ping -c2 -w1 10.0.0.$i >/dev/null 2>&1
[ $? -eq 0 ] && echo 10.0.0.$i
} &
done
wait
echo "在线取IP完成"
案例:批量创建10个用户
1)前缀为自己输入的字符
输入用户处加判断是否为空
2)创建用户个数 判断是否是整数
3)用户密码统一使用123456
read -p "please input prefix:" pre
判断不能为空
read -p "please input number:" num
判断必须是整数
4)用户添加成功 则输出create is ok 失败提示 error
for i in `seq 10`;do userdel -r $pre$i;done
[root@web01 /server/scripts]# cat user.sh
#!/bin/sh
##create abin 2020-6-15
read -p "please input prefix:" pre
[ -z $pre ] && echo "请输入前缀名" && exit
read -p "please input number:" num
[ ! $num =~ ^[0-9]+$ ] && echo "请输入整数" && exit
for i in `seq $num`
do
useradd $pre$i >/dev/null 2>&1
[ $? -eq 0 ] && echo "$pre$i create is ok" || echo "create is error"
echo 123456|passwd --stdin $pre$i >/dev/null 2>&1
done
批量删除用户
for i in `seq 10`;do userdel -r abins$i;done
2.if判断
##单分支
if [ 你有钱 ]
then
echo "我就嫁给你"
fi
##双分支:相当于&& ||
if [ 你有钱 ]
then
echo "我就嫁给你"
elif [ 你有房 ]
then
echo "我也嫁给你"
fi
##多分支
if [ 你有钱 ];then
echo ""
elif [ 你有房 ];then
echo ""
elif [ 你有车 ];then
echo ""
elif [ ];then
echo ""
else [ ];then 可以是else 也可以是elif结尾
echo ""
fi
总结:
单分支 一个条件 一个结果
双分支 一个条件 两个结果
多分支 多个条件 多个结果
案例:判断输入的两个数字的大小 输出结果
[root@web01 /server/scripts]# cat diffnum.sh
read -p "请输入两个整数:" num1 num2
if [ $num1 -gt $num2 ];then
echo "$num1 > $num2"
elif [ $num1 -lt $num2 ];then
echo "$num1 < $num2"
else
echo "$num1 = $num2"
fi
案例:猜数字 先输出一个随机数,read 猜随机数
如果你输入的小了就提示小了 如果大了就提示大了 如果对了则提示 猜对了
[root@web01 /server/scripts]# cat caisz.sh
#!/bin/sh
##create abin 2020-6-15
ran=`echo $((RANDOM%+1))`
while true
do
let i++
read -p "请输入你要猜的数字:" num
if [ $num -gt $ran ];then
echo "比随机数大了"
elif [ $num -lt $ran ];then
echo "比随机数小了"
else
echo "恭喜你 猜对了 总共猜了$i次"
exit
fi
done
[root@web01 /server/scripts]# sh caisz.sh
请输入你要猜的数字:50
比随机数大了
请输入你要猜的数字:25
比随机数大了
请输入你要猜的数字:10
比随机数大了
请输入你要猜的数字:5
比随机数大了
请输入你要猜的数字:0
恭喜你 猜对了 总共猜了5次
案例:按照不同的centos版本 安装不同的yum源
1)当前什么版本 如何取出来
2)if判断 如果是6 则安装6的yum源 7安装7yum源 5的安装5的yum源
[root@web01 /server/scripts]# cat osversion.sh
#!/bin/sh
##create abin 2020-6-15
os=`cat /etc/redhat-release|awk '{print $(NF-1)}'`
[ "$os" == "xxx" ] && os=`取centos6版本的命令`
if [ ${os%%.*} -eq 7 ];then
which wget >/dev/null 2>&1
[ $? -ne 0 ] && yum -y install wget
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
elif [ ${os%%.*} -eq 6 ];then
which wget >/dev/null 2>&1
[ $? -ne 0 ] && yum -y install wget
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-6.repo
else
which wget >/dev/null 2>&1
[ $? -ne 0 ] && yum install wget
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
fi
案例:安装不同版本的PHP 5.5 7.1 7.2 (未完成)
[root@web01 /server/scripts]# cat menu.sh
#!/bin/sh
##create abin 2020-6-15
echo -e "\t\t\t######### 1.PHP install 5.5 #####"
echo -e "\t\t\t######### 2.PHP install 5.7 #####"
echo -e "\t\t\t######### 3.PHP install 7.1 #####"
echo -e "\t\t\t######### 4.PHP install 7.2 #####"
cat <<EOF
1.INSTALL PHP5.5
2.INSTALL PHP5.6
3.INSTALL PHP7.1
4.INSTALL PHP7.2
EOF
read -p "请输入你要安装的版本号" num
if [ $sum -eq 1 ];then yum install -y php5.5
面试题:
作业1:毕业项目案例:如何统计昨天一整天的数据 发送给领导 使用Nginx 或者apache
作业2:安装cacti流量监控 Linux+cacti 网上下载
作业3:安装CRT并可连接服务器
网络命令
dstat 实时查看网络流量
笔试题:Linux添加默认路由题:使用脚本 统计出系统的名称 版本号 内核版本 虚拟平台 eth1IP地址 eth0 IP地址 外网IP地址
[root@web01 /server/scripts]# cat os.sh
#!/bin/sh
System=$(hostnamectl |grep System|awk '{print $3,$4,$5}')
Kernel=$(hostnamectl|grep Kernel|awk -F: '{print $2}')
Vt=$(hostnamectl|grep Virtualization|awk '{print $2}')
Statichostname=$(hostnamectl|grep "Static hostname"|awk '{print $3}')
Eth0=$(ifconfig eth0|awk 'NR==2{print $2}')
Lo=$(ifconfig lo|awk 'NR==2{print $2}')
Network_T=$(curl -s icanhazip.com)
echo "当前系统版本是:$System"
echo "当前系统内核是:$Kernel"
echo "当前虚拟平台是:$Vt"
echo "当前静态主机名是:$Statichostname"
echo "当前Eth0IP地址是:$Eth0"
echo "当前lo地址是:$Lo"
echo "当前外网地址是:$Network_T"
[root@web01 /server/scripts]# sh os.sh
当前系统版本是:CentOS Linux 7
当前系统内核是: Linux 3.10.0-862.el7.x86_64
当前虚拟平台是:vmware
当前静态主机名是:web01
当前Eth0IP地址是:10.0.0.7
当前lo地址是:127.0.0.1
当前外网地址是:117.157.169.44