1、打印九九乘法表
方法一
#外层循环表示9行
for row in `seq 9`
do
#里层循环表示列
for col in `seq $row`
do
echo -e $row x $col = "$[ row*col ]\t\c"
done
echo
done
方法二:
for((i=1;i<=9;i++));do for((j=1;j<=i;j++));do echo -ne "$j X $i = $((i*j))\t";done;echo;done;
方法三:awk写法
awk 'BEGIN{for(i=1;i<=9;i++){for(j=1;j<=i;j++){printf "%s %-4d","j x i =",i*j}printf "\n"}}'
2、国际象棋棋盘
#输入1为两个空格
read -p "输入每个方格的大小(2space=1):" weight
#八行国际象棋
for i in `seq 8`;do
for j in `seq $weight`;do
#奇数行为红 绿 红 绿 ....
if [ "$[i%2]" = 1 ];then
colour1=41 #红色
colour2=42 #绿色
else #偶数行为 绿红 绿红 ....
colour1=42
colour2=41
fi
for k in `seq 4`;do #每行8个方块
for n in `seq $weight`;do #打印2个方块
echo -ne "\033[${colour1}m \033[0m"
done
for m in `seq $weight`;do #打印2个方块
echo -ne "\033[${colour2}m \033[0m"
done
done
echo
3、打印等腰三角形
read -p "请输入三角形的行数:" line
for i in `seq $line`
do
space=`echo $[line-i]`
for j in `seq $space`
do
echo -n ' '
done
for k in `seq $[$i*2-1]`
do
echo -n '*'
done
echo
done
num=9
for i in `seq 1 $num`;do
let j=2*$i-1
let m=10-$i
for l in `seq 1 $m` ;do
echo -e " \c"
done
for k in `seq 1 $j`;do
let n=$i%2
if [ $n -eq 0 ];then
echo -ne "\e[1;5;3$[i]m$i\e[0m"
elif [[ $k -eq 1||$k -eq $j ]];then
echo -ne "\e[1;5;3$[i]m$i\e[0m"
else
echo -ne "\e[1;3$[i]m$i\e[0m"
fi
done
echo
done
unset i
unset j
unset k
unset l
let j=$num/2-1
for i in `seq 1 $j`;do
echo -e "\e[1;3$[i]m 666\\e[0m"
done
echo -e "\e[1;34m 66666\\e[0m"
#输入三角形边长(n个*)
read -p "Please input the triangle side " n
#遍历n行三角形
for i in `seq $n`
do
#打印空格
for j in `seq $[n-i]`
do
echo -e " \c"
done
#除第一行外打印一个星号
if [[ "$i" -ne 1 ]]; then
echo -e "*\c"
fi
#除第一行和最后一行打印空格
if [[ "$i" -ne 1 && "$i" -ne "$n" ]]; then
{
for k in `seq $[2*(i-1) -1]`
do
echo -e " \c"
done
}
fi
#最后一行打印星号
if [[ "$i" -eq "$n" ]]; then
{
k=0
for k in `seq $[2*i-3]`
do
echo -e "*\c"
done
}
fi
echo -e "*"
done
6、每隔30秒监控httpd服务是否启动,如果没启动就启动httpd服务
sleeptime=30
while true
do
if killall -0 httpd &> /dev/null;then
true
else
systemctl restart httpd
time=`date +"%F %T" `
echo "httpd service at "$time" is restarted" >> /app/http.log
fi
sleep "$sleeptime"
done
让脚本在后台执行不影响前台正常工作
nohup ./root/scripts/monhttpd.sh &
7、将连接服务器次数很多次的客户端IP加入防火墙
while true
do
ss -nt | grep ESTAB | tr -s " " ":" | cut -d: -f6 | sort | uniq -c | while read num ip
do
if [ "$num" -gt 2 ];then
iptables -A INPUT -s "$ip" -j REJECT
fi
done
sleep 3
done
8、后续六个字符串:efbaf275cd、4be9c40b8b、 44b2395c46、f8c8873ce0、b902c16c8b、ad865d2f63是通过对随机数变量RANDOM随机执行命令:
echo $RANDOM|md5sum|cut –c1-10
i=1
while [ "$i" -le 32767 ];do
ranmd5=`echo $i | md5sum | cut -c1-10`
case ${ranmd5} in
efbaf275cd)
echo $i md5 is efbaf275cd
;;
4be9c40b8b)
echo $i md5 is 4be9c40b8b
;;
44b2395c46)
echo $i md5 is 4be9c40b8b
;;
f8c8873ce0)
echo $i md5 is 4be9c40b8b
;;
b902c16c8b)
echo $i md5 is 4be9c40b8b
;;
ad865d2f63)
echo $i md5 is 4be9c40b8b
;;
esac
let i++;
done
9、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大值和最小值(随机数最大值32767)
i=1
while [ "$i" -le 10 ];do
random=`echo $RANDOM`
echo $random
if [ "$i" = 1 ];then
max=$random
min=$random
else
if [ "$random" -gt "$max" ];then
max="$random"
elif [ "$random" -lt "$min" ];then
min="$random"
fi
fi
let i+=1
done
echo "max is $max"
echo "min is $min"
10、while循环创建菜单
cat <
select 循环主要用于创建菜单,按数字顺序排列的菜单项将显示在标准错误上,并显示 PS3 提示符,等待用户输入
PS3="please input a menu number: "
select num in huimian mers jdt jikuai quit;do
reply=`echo $REPLY`
echo "--------- $reply"
case "$reply" in
1|2|huimian|mers)
echo "The price is 15"
;;
3|jdt)
echo "The price is 50"
;;
4|jikuai)
echo "The is is 30"
;;
5)
break
;;
*)
echo "your input is $reply"
echo "please input again "
;;
esac
done
11、求100以内所有奇数之和
for ((sum=0,i=1;i<100;i+=2));do let sum+=i;done;echo $sum
12、求100以内所有能被3整除的数之和
sum=0;i=0;while [ $i -lt 100 ];do let sum+=i;let i+=3;done; echo $sum
13、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出
方法一
random=`echo $[RANDOM%10]`
read -p "please guess a num: " num
while [ $num -ne $random ];do
if [ $num -gt $random ];then
echo "your num is too big"
read -p "please guess a num again: " num
elif [ $num -lt $random ];then
echo "your num is too small"
read -p "please guess a num again : " num
fi
done
echo -e "\033[31mcongratulation! \033[31m"