1、创建列表用户并设定初始密码123456
ulist=$(cat /root/users.txt)
for uname in $ulist
do
if [ $uname==`grep $uname /etc/passwd | awk -F: '{print $1}'` ]
then echo "姓名重复"
fi
useradd $uname
echo 123456 | passwd --stdin $uname &> /dev/null
done
2、删除列表用户,并备份用户的文件
ulist=$(cat /root/users.txt)
for uname in $ulist
do
a=`ls -lh /home/$uname | awk -F: '{print $2}'`
if [ "$a" != "0" ]
then cp -r /home/$uname /root/baseup.txt
fi
userdel -r $uname &> /dev/null
done
3、测试ip的连通性
hlist=$(cat /root/ip.txt)
for ip in $hlist
do
ping -c 3 -i 0.2 -W 3 $ip &> /dev/null
if [ $? -eq 0 ]
then echo "Host $ip is up"
else echo "Host $ip is down"
fi
done
4、根据给定的列表用户,给用户的加班工资加100元
list=$(cat /root/users.txt)
base_money=3000
for uname in $list
do
money=$(expr $base_money + 100)
echo "$uname的工资为:$money"
done
5、根据给定的列表用户,给用户的加班工资加100元(位置变量)
base_money=3000
for uname in $1
do
money=$(expr $base_money + 100 \* $2)
echo "$uname的工资为:$money"
done
6、添加20个用户,名字依次为stu1,stu2……stu20
i=1
while [ $i -le 20 ]
do
uname="stu$i"
if [ `grep $uname /etc/passwd | awk -F: '{print $1}'` ]
then echo "名字重复"
fi
useradd $uname
echo 123456 | passwd --stdin $uname $> /dev/null
let i++
done
7、批量删除
i=1
while [ $i -le 20 ]
do
uname="stu$i"
a=`ls -lh /home/$uname | awk -F: '{print $2}'`
if [ "$a" = "0" ]
then cp -r /home/$uname /root/baskup.txt
fi
userdel -r $uname
let i++
done
8、猜价格
price=$(expr $RANDOM % 1000)
times=0
echo "商品实际价格是0~999,猜猜价格是多少?"
while true
do
read -p "请输入你猜测的价格:" INT
if [ $INT -eq $price ]
then echo "恭喜你猜对了,实际价格是$price,总共猜了$times 次"
elif [ $INT -lt $price ]
then echo "太低了"
else
echo "太高了"
fi
let times++
done
9、计算1~50的和
i=0;s=0
until [ $i -eq 50 ]
do
let "i=$i+1";let "s=$i+$s"
done
echo "1~50的和是$s"
转载请注明原文地址:https://blackberry.8miu.com/read-1939.html