1、写一个脚本,使用ping命令探测10.1.8.1-10.1.8.10之间所有主机的在线状态,在线的主机使用绿色显示,不在线的主机使用红色显示。
#!/bin/bash
#Test host whether online
#on-line Green "\033[32m * \033[0m "
#not online red "\033[31m * \033[0m"
for i in {1..10};do
if ping -W 1 -c 1 10.1.8.$i &> /dev/null;then
echo -e "\033[32m 10.1.8.$i is online \033[0m"
else
echo -e "\033[31m 10.1.8.$i is not online \033[0m"
fi
done
执行结果:

2、写一个脚本,通过命令行传递一个参数给脚本,参数为用户名,如果用户的ID号大于等于500,则显示此用户为普通用户。
#!/bin/bash # if [ -z $(grep -o "^$1\>" /etc/passwd) ];then echo "Please enter the correct user name" elif [ $(grep "^$1\>" /etc/passwd | cut -d : -f 3) -ge 500 ];then echo "Ordinary user" else echo "System user" fi
3、写一个脚本,添加10个用户user1-user10,密码同用户名,用户不存在时才添加,存在时则跳过,最后显示本次共添加了多少用户。
#!/bin/bash
#
num=o
for i in {1..10};do
if id user$i &> /dev/null;then
continue
else
useradd user$i
echo "user$i" | passwd --stdin user$i &> /dev/null
let num++
fi
done
echo "Add user $num"
原创文章,作者:641348038@qq.com,如若转载,请注明出处:http://www.178linux.com/63890


评论列表(1条)
赞,两个脚本写的都不错~继续加油~