shell脚本存档

utilsh

1、获取文件名为Storage.php的脚本,根据10分位分钟数拉起不同的脚本,通过合理设置crontab,让1h内每个脚本执行一次。

#! /bin/bash

# 入库表互不影响,并行入库,等最迟的脚本执行完毕退出

file_count=0
store_scripts=()

function GET_SCRIPT_FILE(){
    for file in *;
        do
        if [ -f ${file} ] && [ ${file} == "Storage.php" ] ;then
                #获取全部入库脚本的绝对路径
                ((file_count++))
                store_scripts[${file_count}]=$(echo `pwd`/${file})
        elif [ -d ${file} ];then
                cd ${file}
                GET_SCRIPT_FILE
                cd ..
        fi
        done
}

GET_SCRIPT_FILE
echo "There are ${file_count} scripts !"

#取当前的分钟数,模10后可能是 0~5 共6个数
minute=`date '+%M'`
seed=$((10#${minute}/10))
counter=0
for script in ${store_scripts[@]}; do
  echo ${script}
  #当第n个脚本,n%6==seed时执行;这样crontab设置每10分钟启动一次,1h内可以保证所有脚本运行一遍
  if [ $((${counter}%6)) == ${seed} ];then
    echo "Run!"
    php ${script} && echo "${script} Finished!" &
  else
    echo "Random Num is ${seed},and Counter is ${counter},Skip!"
  fi

  let counter=${counter}+1
done

wait

2、删除MySQL中指定前缀名的表

#! /bin/bash

#db名字
db=$1
#table名字
table=$2

params=`echo $#`
echo "your input param is db=$1 and table prefix like $2"


if [ $params -lt 2 ]
then
 echo "Too Many Params,Can Only Accept Two Params"
 exit
fi

#connect mysql and read tb names
var=$(mysql -h192.168.241.175 -P3306 -uhomework -phomework  -e"use '$1';show tables like '$2%';")

count=0
#read table names
for i in $var;
do
 let count=$count+1
 if [ $count -ne 1 -a $count -ne 2 ]
 then
  #delete from db
  echo "deleting ...$i"
#  mysql -h192.168.241.175 -P3306 -uhomework -phomework  -e"use '$1';drop table $i"
 fi
done

3、删除两个db的差集表

#! /bin/bash


#获取线上db中存在的表名列表
online_tables=$(mysql -h192.168.5.58 -P6147 -uzyb_rd_r -ppdrucy2Jw9pu4Su -D homework_fudao -e"show tables;")

local_tables=$(mysql -h192.168.241.175 -P3306 -uhomework -phomework -D homework_fudao -e"show tables;")

count=0
#read table names
for i in $local_tables;
do
 let count=$count+1

 if [ $count -ne 1 -a $count -ne 2 ] && [[ ! "$online_tables" =~ "$i" ]]
 then
  #delete from db
  echo "deleting ...$i"
  #mysql -h192.168.241.175 -P3306 -uhomework -phomework -D homework_fudao -e "drop table $i"
 fi
done

4、七色球

#!/bin/bash

RED_COL='\033[91m'
BLUE_COL='\033[34m'
NONE_COL='\033[0m'

red_ball=""

while :
do
  clear
  echo '--机选双色球--'
  tmp=$[RANDOM%33+1]
  echo "$red_ball" | grep -q -w $tmp && continue


  red_ball+=" $tmp"

  echo -en "$RED_COL$red_ball$NONE_COL"
  count=$(echo "$red_ball" | wc -w)
  if [ $count -eq 6 ];then
    bule_ball=$[RANDOM%16+1]
    echo -e "$BLUE_COL $bule_ball$NONE_COL"
    break
fi

sleep 1
done

5、es快速查询

#!/bin/bash


INSTANCE="es.basic.suanshubang.com:9220"
AUTH=
ACTION="search"
INDEX=""
BODY="$3"
ISONLINE=false
METHOD="GET"

is_true()           { [[ "$1" = 'true' ]];      }


swan() {
    INSTANCE=es.basic.suanshubang.com:9220
    ACTION=_doc/_search
    INDEX=zyb_swan_workflow;
    BODY="${2:-}"

    is_true $ISONLINE && { INSTANCE=10.33.132.168:9200; AUTH=zybkefu:zybkefu; }

    case "$1" in
        search) ;;
        get)    BODY='{"query":{"match":{"taskId":'$2'}}}';;
        set)    BODY="$2";METHOD="POST"; ACTION=_doc/_update_by_query;;
        index)  ACTION=_doc/_mapping;;

        *) echo "请指定action"; exit 1;;
    esac

}
cuckoo() {
    INDEX=zyb_cuckoo_knowledge;
    ACTION=_doc/_search
    BODY="${2:-}"
    is_true $ISONLINE && { INSTANCE=bzres-f.zuoyebang.cc:80; AUTH=zybkefu:zybkefu; }
    is_true $ISONLINE && { INSTANCE=10.33.132.168:9200; AUTH=zybkefu:zybkefu; }

    case "$1" in
        search) ;;
        get)    BODY='{"query":{"match":{"taskId":'$2'}}}';;
        set)    BODY="$2";METHOD="POST"; ACTION=_doc/_update_by_query;;
        index)  ACTION=_doc/_mapping;;

        *) echo "请指定action"; exit 1;;
    esac
}
set -- $(getopt e "$@")

while [ -n "$1" ]; do
    case "$1" in
        -e) ISONLINE=true;;
        --) shift; break ;;
        *) echo "$1 is not option";exit 1;;
    esac
    shift
done

MODULE="$1"
shift


case "$MODULE" in
    swan) swan "$@";;
    cuckoo) cuckoo "$@";;

    *)   echo "请指定模块";exit 2;;
esac

set -- "http://$INSTANCE/$INDEX/$ACTION" -H 'Content-Type: application/json' -d "$BODY" -X $METHOD

if [ -n "$AUTH" ]; then
    set -- "$@" -H "Authorization: Basic $(echo -n "$AUTH" | base64)"
fi

curl "$@"

此处 set -- xx 的作用是 将xx放到参数列表中,可以用$@取全部参数列表。

shell坑

1、value too great for base (error token is "08")
aa=08会被shell当做八进制数字,加个标注10#$aa,解决。
https://www.cnblogs.com/patf/p/4701472.html

2、centos将 sh 软链 /bin/dash,dash不支持大多数的bash语法和数据结构,
在容器中启动shell时,用/bin/bash 代替 sh

将sh指向bash

你可能感兴趣的:(shell脚本存档)