输入一个字符,判断其是数字还是字母还是其他字符
read -p "输入一个字符,并按回车确认" key
case $key in
[A-Z]|[a-z])
echo "输入的是字母"
;;
[0-9])
echo "输入的是数字"
;;
*)
echo "输入的是其他字符"
;;
esac
剪刀石头布小游戏
read -p "请输入 1剪刀 2石头 3布子:" num
com=`echo $[RANDOM%3+1]`
if [ $num -eq 1 -a $com -eq 3 -o $num -eq 2 -a $com -eq 1 -o $num -eq 3 -a $com
then
echo 人=$num
echo 电脑=$com
echo "你赢了!"
elif [ $num -eq $com ]
then
echo 人=$num
echo "你赢了!"
elif [ $num -eq $com ]
then
echo 人=$num
echo 电脑=$com
echo "平局"
else
echo 人=$num
echo 电脑=$com
echo "机器赢了"
fi
YES或NO
#!/bin/bash
read -p "你好吗? yes/no " input
case $input in
[yY]|[yY][eE][sS])
echo "你输入的是yes"
;;
[nN]|[nN][oO])
echo "你输入的是no"
;;
*)
echo "输入有误"
;;
esac
批量增加用户
- 先创建一个mingdan.txt,在里面创建一些用户名字
#!/bin/bash
user=`cat /home/mingdan.txt`
for i in $user
do
useradd $i
echo "1234" | passwd --stdin $i
done
判断成绩(输入分数,判断在哪个范围)
#!/bin/bash
read -p "输入分数" score
case $score in
100)
echo "$score 牛逼了!"
;;
[89][0-9])
echo "$score 表现不错"
;;
[67][0-9])
echo "$score 一般般"
;;
[1-5][0-9])
echo "$score 你个小垃圾"
;;
[0-9])
echo "$score 吔屎啦你"
;;
*)
echo "输入有误,重新输入"
;;
esac
九九乘法表
方法一:
#!/bin/bash
i=1
while [ $i -le 9 ]
do
for (( j=1;j<=9;j++ ))
do
[ $j -le $i ] && echo -n "$i*$j=$((i*j)) "
done
echo ""
let i++
done
🐱👤 方法二:
#!/bin/bash
for j in {1..9}
do
for i in `seq $j`
do
echo -e "${i}*${j}=$[i*j] \t\c"
done
echo
done
🐱👓 方法三(倒三角):
#!/bin/bash
for j in {1..9}
do
for i in `seq $[10-$j]`
do
echo -ne " ${i}x`echo $[10-j]`=$[(10 -j)*i]\t"
done
echo
done
加法(从1加到100)
- 方法一:
#!/bin/bash
sum=0
for i in {1..100}
do
sum=$[i+sum]
done
echo $sum
🐱🚀
- 方法二:
#!/bin/bash
sum=o
for i in {1..100}
do
let sum+=i
done
echo $sum
🐱💻
- 方法三:
#!/bin/bash
sum=0
for ((i=1;i<=100;i++))
do
sum=$[i+sum]
done
echo $sum
🐱👓
- 方法四(1-100内偶数相加之和):
#!/bin/bash
sum=0
for i in {1..100..2}
do
sum=$[i+sum]
done
echo $sum
🐱👤
- 方法五:
awk 'BEGIN { for(i=1; i<=100; i++) sum+=i; print sum}'
🐱🚀
ping测试
#!/bin/bash
for i in {1..254}
do
ping -c 5 192.168.17.$i &>/dev/null
if [ $? -eq 0 ]
then
echo "$i is online"
else
echo "$i is down"
fi
done
查看80端口是否监听,启动httpd服务
#!/bin/bash
netstat -natp | grep :80 &> /dev/null
if [ $? = 0 ]
then
echo "网站服务已运行"
else
rpm -q httpd &> /dev/null
if [ $? -eq 0 ]
then
echo "httpd已安装,并准备启动..."
systemctl start httpd
else
echo "正在安装httpd服务,并准备启动..."
df | grep "/dev/sr0" &> /dev/null
if [ $? -eq 0 ]
then
yum install -y httpd &> /dev/null
systemctl start httpd
else