1.for——批量创建用户
[root@Kiro for]
user=`cat /root/shell/users2.txt`
for name in $user
do
useradd $name
echo "123456"|passwd --stdin $name &> /dev/null
done
2.for——九九乘法表
[root@Kiro for]
for i in {9..1..-1}
do
for j in `seq $i`
do
echo -e -n "${i}x${j}=$[ $i * $j ]\t"
done
echo
done
3. 三次机会猜密码
[root@Kiro for]
init=123456
for i in {1..3}
do
read -p "请输入密码:" pass
if [ $pass == $init ];then
echo "密码正确"
exit
fi
done
4. 根据ip地址列表检查主机状态
#!/bin/bash
IPLIST=$(cat /root/shell/ip.txt)
for IP in $IPLIST
do
ping -c 3 -i 0.5 $IP &> /dev/null
if [ $? -eq 0 ];then
echo "host $IP is up."
else
echo "host $IP is down."
fi
done
5. 判断是否有httpd服务,没有的话立即安装
#!/bin/bash
ps -aux | grep httpd |grep -v grep &>/dev/null
if [ $? -ne 0 ];then
if [[ "$(rpm -q httpd )"=="未安装软件包 httpd" ]];then
echo "正在启动Apache服务..." &>/dev/null
yum install -y httpd &>/dev/null
systemctl restart httpd
echo "Apache服务已经启动"
else
systemctl restart httpd
fi
else
echo "httpd is running"
fi
6. 判断当前用户是否为超级用户
#!/bin/bash
--------字符方式---------
if [ $USER = '/root' ];then
echo "您为超级用户,准备安装vsftpd"
yum install -y vsftpd &>/dev/null
systemctl start vsftpd
echo "正在启动vsftpd服务........"
else
echo "您不是管理员!"
fi
--------UID方式---------
if [ $UID -eq 0 ];then
echo "当前用户超级用户!"
yum install -y vsftpd &> /dev/null
systemctl start vsftpd
echo "正在启动vsftpd服务中........"
else
echo "您不是超级用户!"
fi
7. 根据月份判别季节
#!/bin/bash
read -p "输入月份判别季节:" month
case $month in
[1-3])
echo "$month月 是春天!"
;;
[4-6])
echo "$month月 是夏天!"
;;
[7-9])
echo "$month月 是秋天!"
;;
1[0-2])
echo "$month月 是冬天!"
;;
*)
echo "输入有误!请输入1-12!"
esac
8.查找/var/log 中文件并统计数量
#!/bin/bash
cd /var/log/
num=`ls -l | wc -l`
echo "$num"
a=0
for ((i=0;i<$num;i++))
do
file=`find /var/log -name "*.log" -type f `
if [ $? -eq 0 ];then
let ++a
else
echo "无此类日志文件"
fi
done
echo "所有的日志文件:$file"
echo "文件数量:$a"
9. 查看网段所在的主机联通情况
net=192.168.61
for i in {1..254}
do
ping -c 3 -i 0.3 $net.$i &> /dev/null
if [ $? -eq 0 ];then
echo "$net.$i is up."
else
echo "$net.$i is dowm."
fi
done
10、输入路径统计文件个数
read -p "请输入路径(直接换行表示当前目录):" fpath
if [ "$fpath" = "" ]
then
fpath=$(pwd)
fi
directory()
{
echo "您输入的路径为:$fpath"
let "filenum = 0"
let "dirnum = 0"
for dir in ${fpath}/*
do
if [ -d "$dir" ]
then
let "dirnum+=1"
elif [ -f "$dir" ]
then
let "filenum+=1"
fi
done
echo "当前目录下文件夹的个数: $dirnum"
echo "当前目录下文件的个数: $filenum"
}
directory
11、猜价格
#!/bin/bash
price=$[RANDOM%1000]
times=0
echo "猜猜价格!"
while true
do
read -p "输入你猜测的价格:" num
let times++
if [ $num -eq $price ];then
echo "恭喜你答对啦!"
echo "你总共猜了$times次"
break
elif [ $num -lt $price ];then
echo "有点小了!再猜猜!"
elif [ $num -gt $price ];then
echo "有点大了!再猜猜!"
else
echo "输入有误!"
fi
done
12、生成随机密码
==使用mkpasswd命令需要先安装expect
[root@Kiro homework]
mkpasswd -l 6 -s 1 -C 1 -c 1 -d 1
|