shell代码模板
Posted 冰山上的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 "[email protected]${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 ‘run‘
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 DaysToDownload"
echo "Example: sh $0 20140831 7, this will download logs for 7 days: 20140825-20140831."
echo ""
exit
fi
# Step 2: Fetch log data
last_day=$1
days_to_download=$2
for((i = 1; i <= ${days_to_download} ; ++i));
do
(( finished = i - 1))
(( togo = ${days_to_download} - finished ))
echo ""
echo "-------"
if [ ${last_day} = "yesterday" ]; then
day=`date -d -${i}day +%Y%m%d`
else
day=`date -d "${last_day} ${finished} days ago" +%Y%m%d`
fi
echo "[`date`] Downloading log for ${day}, ${finished} finished, ${togo} to go......"
start_date=`date`
for ((j=1; j<=3; j++));
do
# download logs
# download logs finished
if [ $? -eq 0 ]; then
break
fi
done
end_date=`date`
echo "Finished ${day}. Started at ${start_date}, finished at ${end_date}"
done
echo ""
echo "Finished downloading $2 logs ( ${day} - ${last_day} ) !"
echo ""
例行拉取日志(forwardtrack.sh)
#!/bin/bash
# Step 1: Check Parameters
if [ $# -lt 2 ]; then
echo ""
echo "Usage: sh $0 FirstDay DaysToDownload"
echo "Example: sh $0 20140825 7, this will download logs for 7 days: 20140825-20140831."
echo ""
exit
fi
# Step 2: Fetch log data
first_day=$1
days_to_download=$2
for ((i=1; i<=${days_to_download}; i++));
do
(( finished = i - 1))
(( togo = ${days_to_download} - finished ))
echo ""
echo "-------"
day=`date -d "${first_day} ${finished} days" +%Y%m%d`
echo "[`date`] Downloading log for ${day}, ${finished} finished, ${togo} to go......"
start_date=`date`
for ((j=1; j<=3; j++));
do
# download logs
# download logs finished
if [ $? -eq 0 ]; then
break
fi
done
end_date=`date`
echo "Finished ${day}. Started at ${start_date}, finished at ${end_date}"
done
echo ""
echo "Finished downloading $2 logs ( ${first_day} - ${day} ) !"
echo ""