shell小脚本集合

拥有回忆 提交于 2020-01-26 19:58:33

1.猜数字

#!/bin/bash
x=$[RANDOM%10+1]
while read -p "please your number1-10:" num;do
   #echo $num
    if [ "$num" -eq "$x" ];then
       echo your gueess right.
       break
    elif [ "$num" -lt "$x" ];then
       echo letter
    else
       echo  more
    fi
done

2.棋盘

#!/binbash
red="\033[41m"
blue="\033[44m"
cloend="\033[0m"
#echo -e "$red  $cloend"
for j in {1..8};do
  if [ $[j%2] -eq 1 ];then
   for i in {1..8};do
       if [ $[i%2] -eq 1 ];then
            echo -e "$red  $cloend\c"
       else
           echo -e "$blue  $cloend\c"
       fi
   done
  elif [ $[j%2] -eq 0 ];then
      for k in {1..8};do
         if [ $[k%2] -eq 0 ];then
           echo -e "$red  $cloend\c"
         else
           echo -e "$blue  $cloend\c"
         fi
      done
   fi
      echo
done

3.九九乘法表

#!/bin/bash
for i in {1..9};do
    for j in $(seq $i);do
       let k=i*j
       echo -e "$i x $j = $k\t\c"
       #echo -e "${j}x$i=$[j*i]\t\c"
    done
    echo
done

4.echo yes or no

#!/bin/bash
read -p "Input yes or no: " yn
[[ "$yn" =~ ^[yY]([Ee][Ss])?$ ]] && { echo yes;exit 0; }
[[ "$yn" =~ ^[Nn][Oo]?$ ]] && { echo no;exit 1; }
echo please input yes or no.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!