shell代码模板

批量ssh登录机器
#site_search_hosts 10.4.16.205,10.4.20.87,10.4.20.88,10.4.20.89,10.4.20.90,10.4.20.92,10.4.20.93,10.4.21.51,10.4.21.52,10.4.21.53,10.4.21.54,10.4.33.136,10.4.33.137,10.4.33.138,10.4.33.139,10.4.33.140
site_search_hosts='10.4.16.205,10.4.20.87,10.4.20.88,10.4.20.89,10.4.20.90,10.4.20.92,10.4.20.93,10.4.21.51,10.4.21.52,10.4.21.53,10.4.21.54,10.4.33.136,10.4.33.137,10.4.33.138,10.4.33.139,10.4.33.140'
NODE_HOSTS=(${site_search_hosts//,/ })
 
#NODE_HOSTS=(
#       10.4.33.136
#       10.4.33.137
#       10.4.33.138
#       10.4.33.139
#       10.4.33.140
#       )
NODE_HOSTS_CNT=${#NODE_HOSTS[@]}
 
echo $NODE_HOSTS_CNT
 
for (( i=0; $i<${NODE_HOSTS_CNT}; i=$i+1 ))
do
        ssh "tiger@${NODE_HOSTS[$i]}" "hostname -i; \
 
        cd .service
        ls
done

 

例行
lastday=''
while [ 1 ]; do
    day=`date +%Y%m%d`
    hour=`date +%H`
    echo $lastday
    echo $day
    echo $hour
    if [ "${day}" != "${lastday}" -a ${hour} -eq 6 ]; then
        cur_date=`date -d -1day +%Y%m%d`
        echo 'do task'
        lastday=$day
    fi
    sleep 600
    echo "done"
done
 
读入文件
n=0
CONF_FILE="action_needed.conf"
cat $CONF_FILE |
        while read LINE
        do
                echo "$n) $LINE"
                n=`expr $n + 1`
        done
 
字符串处理
$ date=`date`
$ echo $date
Mon Nov 30 10:52:09 CST 2015
$ echo ${date#* * * }
10:52:09 CST 2015
$ t1=${date#* * * } 
$ echo ${t1%:*:*} 
10
 
#First, get file name without the path:
filename=$(basename "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"
#Alternatively, you can focus on the last '/' of the path instead of the '.' which should work even if you have unpredictable file extensions:
filename="${fullfile##*/}"
 
正则表达式匹配
s="hdfs://hdfs.example.com:8800/app/yaoyao/20141203/part-00042"
if [[ $s =~ [0-9]{8,8} ]]; then
        echo ${BASH_REMATCH[0]}
fi
 
向前回溯天级任务(backtrack.sh)
#!/bin/bash
 
# Step 1: check parameters
if [ $# -lt 2 ]; then
        echo ""
        echo "Usage: sh $0 lastDay daysOfTask"
        echo "Example: sh $0 20140831 7, this will do tasks for 7 days: 20140825-20140831."
        echo ""
 
        exit
fi
 
# Step 2: do tasks
last_day=$1
days_of_task=$2
for((i = 1; i <= ${days_of_task} ; ++i));
do
        (( finished =  i - 1))
        (( todo =  ${days_of_task} - finished ))
 
        echo ""
        echo "-------" 
 
        if [ ${last_day} = "yesterday" ]; then
                proc_day=`date -d -${i}day +%Y%m%d`
        else
                proc_day=`date -d "${last_day} ${finished} days ago" +%Y%m%d`
        fi
 
        echo "[`date`] Doing task for ${proc_day}, ${finished} finished, ${todo} to do......"
 
        start_date=`date`
# retry 3 times
for ((j=1; j<=3; j++)); do # do task # task finished if [ $? -eq 0 ]; then break fi done end_date=`date` echo "Finished ${proc_day}. Started at ${start_date}, finished at ${end_date}" done echo "" echo "Finished done $2 tasks ( ${proc_day} - ${last_day} ) !" echo ""
 
向后回溯天级任务(forwardtrack.sh)
#!/bin/bash
 
# Step 1: check parameters
if [ $# -lt 2 ]; then
        echo ""
        echo "Usage: sh $0 firstDay daysOfTask"
        echo "Example: sh $0 20140825 7, this will do tasks for 7 days: 20140825-20140831."
        echo ""
 
        exit
fi
 
# Step 2: do tasks
first_day=$1
days_of_task=$2
for ((i=1; i<=${days_of_task}; i++));
do
        (( finished = i - 1))
        (( todo = ${days_of_task} - finished ))
 
        echo ""
        echo "-------"
 
        proc_day=`date -d "${first_day} ${finished} days" +%Y%m%d`
 
        echo "[`date`] Doing task for ${proc_day}, ${finished} finished, ${todo} to do......"
 
        start_date=`date`
# retry 3 times
for ((j=1; j<=3; j++)); do # do task # task finished if [ $? -eq 0 ]; then break fi done end_date=`date` echo "Finished ${proc_day}. Started at ${start_date}, finished at ${end_date}" done echo "" echo "Finished done $2 tasks ( ${first_day} - ${proc_day} ) !" echo ""

 

判断星期几

week_day=`date +"%w"`

for i in 0 2 4 6
do
    if [ ${week_day} -eq $i ]; then
        echo 'generate data for Monday/Wednesday/Friday'
        exit 0
    fi
done;

 

posted on 2019-02-11 21:00  冰山上的博客  阅读(478)  评论(0编辑  收藏  举报