shell小脚本集合

目录

    • 0.vim脚本规范
    • 1.猜数字
    • 2.棋盘
    • 3.九九乘法表
    • 4.echo yes or no
    • 5.复制Linux命令和库
    • 6.斐波那契数列(兔子数列)
    • 7.冒泡法排序
    • 8.等腰三角形
    • 9.生成证书和授权

0.vim脚本规范

vi ~/.vimrc

autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
         if expand("%:e")=='sh'
             call setline(1,"#!/bin/bash")
             call setline(2,"#")
             call setline(3,"#*************************************")
             call setline(4,"#author:                zyx")
             call setline(5,"#file:                  ".expand("%"))
             call setline(6,"#email:                 [email protected]")
             call setline(7,"#version:               1.0")
             call setline(8,"#date:                  ".strftime("%Y-%m-%d"))
             call setline(9,"#description:           test script")
             call setline(10,"#*************************************")
         endif
endfunc

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.

5.复制Linux命令和库

#!/bin/bash

f_dir=/mnt/sysroot
#判断指令是否存在
input_cmd(){
    while :;do
 read -p "plsease input cmd or quit: " cmd
       if [ -z "$cmd" ];then
       echo please input cmd
       continue
    elif [ $cmd == "quit" ];then
       exit 88
    elif  ! which $cmd &> /dev/null;then
       echo no $cmd command
    continue
    else
       break
    fi
 done
}
#复制命令
cp_cmd(){
    cmd_name=`which --skip-alias $cmd`
    cmd_dir=`dirname ${cmd_name}`
 [ -d ${f_dir}${cmd_dir} ] || mkdir -p ${f_dir}${cmd_dir}
 [ ! -f ${f_dir}${cmd_name} ] && { cp  ${cmd_name} ${f_dir}${cmd_name};echo $cmd copy finshed.; } || echo $cmd is exist.
}
#复制命令库
cp_cmd_lib(){
    for lib_name in `ldd /usr/bin/$cmd | sed -r '1d;s/.*[[:space:]](\/.*) .*/\1/g'`;do
     lib_dir=`dirname ${lib_name}`
  [ -d ${f_dir}${lib_dir} ] || mkdir -p ${f_dir}${lib_dir}
     [ -f ${f_dir}${lib_name} ] || cp  ${lib_name} ${f_dir}${lib_name}
 done
    echo "backup ${cmd}'s library finished"
}

main(){
    while :;do
       input_cmd
       cp_cmd
       cp_cmd_lib
    done
}

main

6.斐波那契数列(兔子数列)

#!/bin/bash
fact(){
   if [ $1 -eq 0 ];then
      echo 0
   elif [ $1 -eq 1 ];then
      echo 1
   else
      echo $[`fact $[$1-1]`+`fact $[$1-2]`]
   fi
}

fact $1
#f(n)=f(n-1)+f(n-2)

7.冒泡法排序

#!/bin/bash
declare -a arry
read -p "plaeas input some numbers: " -a arry
for((c=1;c<=${#arry[@]};c++));do
   for i in $(seq 1 $[${#arry[*]}-1]);do
      if [ ${arry[$[$i-1]]} -gt ${arry[$i]} ];then
      #两个数用异或对调
          let arry[$[$i-1]]=arry[$[$i-1]]^arry[$i]
          let arry[$i]=arry[$[$i-1]]^arry[$i]
          let arry[$[$i-1]]=arry[$[$i-1]]^arry[$i]
      fi
   done
done
echo ${arry[@]}

8.等腰三角形

#!/bin/bash
read -p "Please input the length: " n:
for i in `seq 1 $n`
do
   for((j=$n;j>i;j--))
   do
      echo -n  "@"
   done
   for k in $(seq 1 $[2*$i-1]);do
      echo -n "*"
   done
   echo
done

9.生成证书和授权

#!/bin/bash
dir=/etc/pki/CA

# 建立CA
ca_build(){
    umask 077 &&
    openssl genrsa -out ${dir}/private/cakey.pem 4096 &&
    openssl req -new -x509 -key ${dir}/private/cakey.pem -out ${dir}/cacert.pem -days 365 < ${dir}/serial &&
    echo ca build successful.
    }

# 申请证书
ca_apply(){
    umask 066 &&
    openssl genrsa -out /data/test.key 1024
    openssl req -new -key /data/test.key  -out ${dir}/test.csr <

你可能感兴趣的:(shell小脚本集合)