文章目录
100米赛跑,10秒内进决赛,分男女组
#!/bin/bash
read -p "请输入你跑步的成绩:" nu
case "$nu" in
10|[0-9])
read -p "请输入你的性别:" s
case "$s" in
man)
echo "进入男子组决赛"
;;
women)
echo "进入女子组决赛"
;;
*)
echo "请输入正确的性别"
esac
;;
*)
echo "淘汰出局"
esac
石头剪刀布
#!/bin/bash
game=(石头 剪刀 布)
num=$[RANDOM%3]
computer=${game[$num]}
echo "请根据下列提示选择您的出拳手势"
echo "1.石头"
echo "2.剪刀"
echo "3.布"
read -p "请选择1-3:" person
case $person in
1)
if [ $num -eq 0 ];then
echo "平局"
elif [ $num -eq 1 ];then
echo "你赢"
else
echo "计算机赢"
fi;;
2)
if [ $num -eq 0 ];then
echo "计算机赢"
elif [ $num -eq 1 ];then
echo "平局"
else
echo "你赢"
fi;;
3)
if [ $num -eq 0 ];then
echo "你赢"
elif [ $num -eq 1 ];then
echo "计算机赢"
else
echo "平局"
fi;;
*)
echo "必须输入1-3的数字"
esac
until语句,做1-50内相加
#!/bin/bash
i=1
sum=0
until [ $i -eq 51 ]
do
let sum+=$i
let i++
done
echo "$sum"
~
输出1-100中能被2整除的所有数,并求其和
#!/bin/bash
sum=0
for i in `seq 1 100`;do
if [ `expr $i % 2` -eq 0 ];then
echo $i
let sum=$sum+$i
fi
done
echo $sum
根据IP地址范围检查主机在线状态
#!/bin/bash
for i in `seq 131 134`;do
ping -c 3 -i 0.2 -w 3 "192.168.50.$i" > /dev/null
if [ $? -eq 0 ];then
echo "192.168.50.$i is up!"
else
echo "192.168.50.$i is down!"
fi
done
为系统批量添加用户
#/bin/bash
list=`cat /root/1.txt`
for i in $list
do
useradd $i
echo "123456" | passwd --stdin $i > /dev/null
echo "用户${i}添加成功!"
done
输出1-100所有奇数
#!/bin/bash
#
i=1
while [ $i -le 100 ];do
if [ `expr $i % 2` -eq 1 ];then
echo $i
fi
let i++
done
小游戏:猜商品价格
#!/bin/bash
clear
i=1
price=$(expr $RANDOM % 1000)
while true;do
read -p "请输入商品价格:" a
if [ $a -eq $price ];then
echo "恭喜您猜对了!"
break
elif [ $a -gt $price ];then
echo "您猜高了!"
elif [ $a -lt $price ];then
echo "您猜低了!"
fi
let i++
done
echo "您猜了${i}次"
为指定用户发送在线消息
#!/bin/bash
username=$1
if [ $# -lt 1 ];then
echo "请输入正确的用户名!"
exit 0 ## 直接退出程序,不再执行下面的任何语句
fi
if grep "^$username" /etc/passwd > /dev/null;then :
else
echo "用户不存在!"
exit 1
fi
until who | grep "$username" > /dev/null;do
echo "该用户不在线!"
sleep 5 ## 休眠5秒
done
echo "$2" | write "$username"
echo "${username}发送成功!"
cho “用户不存在!”
exit 1
fi
until who | grep “$username” > /dev/null;do
echo “该用户不在线!”
sleep 5 ## 休眠5秒
done
echo “
2
"
∣
w
r
i
t
e
"
2" | write "
2"∣write"username”
echo “${username}发送成功!”