A. Printing 9 * 9 multiplication table
1 #!/bin/bash 2 for i in `seq 9` 3 do 4 for j in `seq $i` 5 do 6 echo -n "$i*$j=$[i*j]" 7 done 8 echo 9 done
II. Rock Paper Scissors game
. 1 #! / Bin / the bash 2 Game = (Janken) . 3 NUM = $ [the RANDOM% . 3 ] . 4 Computer = $ {game [$ num]} . 5 # punches may be randomly generated and stored in the array game [$ num ]: Game [ 0 ], Game [ 1 ], Game [ 2 ] represent the rock, paper, scissors . 6 echo " select gesture punches according to the following prompt " . 7 echo " stone: scissors 1: 2 cloth:. 3 " . 8 Read -p " Please punches: (, 2, 3) " : the Person 9 Case $ the Person in 10 1 ) 11 IF[$ NUM -eq 0 ]; the then 12 is echo " tie " 13 is elif [$ NUM -eq . 1 ]; the then 14 echo " you win " 15 the else 16 echo " Computer Win " . 17 Fi ;; 18 is 2 ) . 19 IF [$ -eq NUM 0 ]; the then 20 is echo " computer win " 21 is elif [$ NUM -eq . 1 ]; the then 22 is echo "Draw " 23 is the else 24 echo " you win " 25 Fi ;; 26 is . 3 ) 27 IF [$ NUM -eq 0 ]; the then 28 echo " you win " 29 elif [$ NUM -eq . 1 ]; the then 30 echo " Computer Win " 31 the else 32 echo " draw "
III. Guess the game
1 #! / Bin / bash 2 # script to generate a random number less than 100, the user is prompted guessing, based on user input,> prompt guessed it, guess guess big or small, until you guess right so far. 3 NUM = $ [the rANDOM% 100 + 1 ] # 1 - the random number 100 . 4 Read -p " computer-generated random number 1 to 100, please guess: " cai1 . 5 L = 0 . 6 the while : . 7 do . 8 the let L ++ . 9 IF [L $ -eq . 1 ]; the then 10 IF [-eq $ $ cai1 NUM]; the then . 11 echo " Congratulations, guessed " 12 Exit 13 is elif [-gt $ $ cai1 NUM]; the then 14 echo " guess big " 15 the else 16 echo " guess small " . 17 Fi 18 is the else . 19 Read -p " continue: " CaI2 20 is IF [$ CaI2 -eq NUM $]; the then 21 echo " Congratulations, you guessed it " 22 Exit 23 elif [$ CaI 2 -gt $ NUM]; the then 24- echo " guess the big " 25 the else 26 echo " Guess small " 27 fi 28 fi 29 DONE
IV. To three random numbers sorting
1. Interactive
1 #!/bin/bash 2 #依次提示用户输入三个整数,脚本根据数字大小排序输出3个数字 3 read -p "请输入一个整数:" num1 4 read -p "请输入一个整数:" num2 5 read -p "请输入一个整数:" num3 6 #从小到大排序,设定最后输出num1,num2,num3,脚本运行中将最小,中间,最大值分别赋予这三个变量,引入对调变量tmp 7 tmp=0 8 #如果num1大于num2则对调1和2,保持num1最小 9 if [ $num1 -gt $num2 ];then 10 tmp=$num1 11 num1=$num2 12 num2=$tmp 13 fi 14 #如果num1大于num3则对调1和3,保持num1最小 15 if [ $num1 -gt $num3 ];then 16 tmp=$num1 17 num1=$num3 18 num3=$tmp 19 fi 20 #如果num2大于num3则对调2和3,保持num2更小 21 if [ $num2 -gt $num3 ];then 22 tmp=$num2 23 num2=$num3 24 num3=$tmp 25 fi 26 echo "排序后的数据为:$num1,$num2,$num3"
2.非交互式
1 #!/binbash 2 l=0 3 tmp=0 4 for i in `cat /home/student/桌面/shell脚本/文档/paixu.txt` #一个含3个数字的文件 6 do 7 let l++ 8 if [ $l -eq 1 ];then 9 num1=$i 10 fi 11 if [ $l -eq 2 ];then 12 num2=$i 13 if [ $num1 -gt $num2 ];then 14 tmp=$num1 15 num1=$num2 16 num2=$tmp 17 fi 18 fi 19 if [ $l -eq 3 ];then 20 num3=$i 21 if [ $num1 -gt $num3 ];then 22 tmp=$num1 23 num1=$num3 24 num3=$tmp 25 fi 26 if [ $num2 -gt $num3 ];then 27 tmp=$num2 28 num2=$num3 29 num3=$tmp 30 fi 31 fi 32 done 33 echo "从小到大排序:$num1,$num2,$num3"
五.点名器
1 #!/bin/bash 2 #提前准备文件user.txt,一行一个姓名 3 read -p "请输入想要抽到的人数:" xxx 4 l=1 5 while [ $l -le $xxx ] 6 do 7 let l++ 8 line=`cat /home/student/桌面/shell脚本/文档/user.txt | wc -l` 9 num=$[RANDOM%line+1] 10 sed -n "${num}p" /home/student/桌面/shell脚本/文档/user.txt 11 sleep 0.5 12 done
六.打印国际象棋棋盘
1 #!/bin/bash 2 #两个变量i和j代表行和列 3 for i in {1..8} 4 do 5 for j in {1..8} 6 do 7 sum=$[i+j] 8 if [ $[sum%2] -eq 0 ];then #偶数 9 echo -ne "\033[46m \033[0m" 10 else 11 echo -ne "\033[47m \033[0m" 12 fi 13 done 14 echo 15 done
七.fork炸弹(会快速消耗计算机资源,导致计算机死机)(请在虚拟机中实验,千万不要在真机尝试,若操作失误请重启计算机)
1 .(){ 2 .|.& 3 } 4 .
八.打印斐波那契数列(后一个数字永远是前两个数字之和)
1 #!/bin/bash 2 list=(0 1) 3 for i in `seq 2 11` 4 do 5 list[$i]=`expr ${list[-1]} + ${list[-2]}` 6 done 7 echo ${list[@]}
九.打印一些特殊图形
1 #!/bin/bash 2 clear 3 for ((i=1;i<=9;i++)) 4 do 5 for((j=1;j<=i;j++)) 6 do 7 echo -n "$i" 8 done 9 echo"" 10 done 11 #图1 12
13 read -n1 "按任意键继续" key 14 clear 15 for ((i=1;i<=5;i++)) 16 do 17 for((j=1;j<=i;j++)) 18 do 19 echo -n "|" 20 done 21 echo "_" 22 done 23 #图2 24
25 read -n1 "按任意键继续" key 26 clear 27 for ((i=1;i<=5;i++)) 28 do 29 for((j=1;j<=i;j++)) 30 do 31 echo -n "*" 32 done 33 echo"" 34 done 35 36 for ((i=5;i>=1;i--)) 37 do 38 for((j=1;j<=i;j++)) 39 do 40 echo -n "*" 41 done 42 echo"" 43 done 44 #图3