常用Shell命令
BASEDIR=$(cd $(dirname $0) && pwd) cd $BASEDIR>/dev/null
选取两个文件中相同的部分:(Must sort first)
comm -12 <(sort /tmp/gcs.calix.com-wifi-pm-per-radio_50|uniq) <(sort /tmp/gcs.calix.com-wifi-pm-per-radio_dupcheck_50|uniq)
CURL:
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":100}' http://localhost/test/adduser
变量处理:
#根据变量值获取变量 pass=`eval echo '$'psql_"${env}_pass"` cmd=`eval echo '$'psql_"$env"`
数值处理:
#判断是否为数字 if [ -n "$org" ] && [ "$org" -eq "$org" ] 2>/dev/null; then echo "$org need process" cat result.txt|grep $org>_result_${org} fi #数字累加
step=`expr "$step" + 1`
输入参数:
usage="Usage: $0 -o/--org orgId[Must] -p/--prepare t[Option] -f/--force t[Option] -d/--dry t[Option] -k/--kafka t[Option]" while [ "$1" != "" ] do case $1 in -o|--org) shift orgId=$1 ;; -f|--force) shift if [ "$1" = "-p" ];then prepare=1 fi force=1 ;; -p|--prepare) shift if [ "$1" = "-f" ];then force=1 fi prepare=1 ;; -d|--dry) shift dry=1 ;; -k|--kafka) shift kafka_flg=$1 ;; *) echo $usage exit 1 ;; esac shift done if [ -z $orgId ];then echo -e "$RED[Error] Missing orgId!$NC $usage" exit fi
确认函数:
check_and_commit() { cmd="" step=`expr "$step" + 1` echo ""|tee -a $logs echo -e "$BLUE[`date +'%Y-%m-%d %H:%M:%S'`][Step $step] exec $1 $NC"|tee -a $log if [ $force -eq 0 ];then while true; do read -p "Do you confirm to execute step $1? [y/n]" yn case $yn in [Yy]* ) $2; if [ $dry -eq 1 ];then echo -e "$GREEN [Dry Run]$NC $cmd"|tee -a $log else echo $cmd|tee -a $log $cmd|tee $logsdir/$1.log fi break;; [Nn]* ) echo "ignore step $1"|tee -a $log ;break;; esac done else $2 if [ $dry -eq 1 ];then echo -e "$GREEN [Dry Run]$NC $cmd"|tee -a $log else echo $cmd|tee -a $log $cmd|tee -a $logsdir/$1.log fi fi }
prepare_message_confirm() { echo "Please make sure next items be done" echo -e "${RED} 1.env.sh use correct environment information ${NC}" echo -e "${RED} 2.all gcs vm had added the onecloud replay URL and restarted${NC}" echo -e "${RED} 3.make sure this vm can connect to brown field mongo/redshift/CMC gateway ${NC}" echo -e "${RED} 4.had startup cloud-subscriber with correct version and expose port 3424 ${NC}" echo -e "${RED} 5.brown field subscrbier-sync pod had patched ${NC}" if [ $force -eq 0 ];then while true; do read -p "Do you confirm ? [y/n]" yn case $yn in [Yy]* ) echo "will continue to execute for org :$orgId";break;; [Nn]* ) exit -1 ;break;; esac done fi }
时间函数:
#根据时区显示时间:
TZ=:Asia/Hong_Kong date +'%Y-%m-%d %H:%M:%S'
#根据long型时间获取字符值
date_str=$(date -d @$time_long +'%Y-%m-%d')
#计算时间差值:
start_date=$1
if [ -z $start_date ];then
start_date=`date --date="-7 day" +%Y-%m-%d`
fi
#end_date=$(date -d "$start_date 1 day" +%Y-%m-%d)
#end_date=`date --date="-1 day" +%Y-%m-%d`
end_date=`date +%Y-%m-%d`
start_time=$(date -d "$start_date" +%s)
end_time=$(date -d $end_date +%s)
字符处理:
#去除文件后缀
fnocsv=${f%%.csv}
cdevice=`echo $fnocsv|awk -F '_' '{print $1}'`
time_long=`echo $fnocsv|awk -F '_' '{print $2}'`
date_str=$(date -d @$time_long +'%Y-%m-%d')
#orgs截取掉最后的orgId
left="${orgs#*$orgId}"
统计网络连接状态:
#netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key," ",state[key]}'
ESTABLISHED 21
FIN_WAIT1 3
SYN_SENT 1
#ss -s
Total: 181 (kernel 0)
TCP: 19 (estab 13, closed 0, orphaned 1, synrecv 0, timewait 0/0), ports 0
Transport Total IP IPv6
* 0 - -
RAW 0 0 0
UDP 6 6 0
TCP 19 17 2
INET 25 23 2
FRAG 0 0 0
数组:
IFS=',' read -ra org_array <<< "$orgs" for orgId in "${org_array[@]}" do
While 循环
for s in `ls _result_*` do while [ `ps -ef|grep pm_missing_data_move.py|grep -v grep|wc -l` -gt 3 ] do sleep 1s done if [ -f $s ];then if [ `ps -ef|grep -v grep|grep $s|wc -l` -eq 0 ];then mv $s run_$s nohup python pm_missing_data_move.py -e tony.ben@calix.com --password FA200417# --filename run_$s && mv run_$s done_${s} & fi fi done
sed函数
#普通替换 sed -i "s/ENV/$env/g" $sql #读取行 sed -n '1,3p' xxx.txt //读取第一到3行 #删除 sed -i '/xx/d' xxx.txt
query="'{"orgId" : "$orgid"}'"
echo "$query"
mongoexport -h 199.71.143.62 -d sxa -c sxa-subscribers -f customId,_id -q '{"orgId" : "145079"}' --csv -o customId.csv
sed -i '/customId/d' customId.csv
sed -i "s/^"/update cloud_subscribers set subscriber_location_id='/" customId.csv
sed -i "s/","/' where subscriber_id ='/" customId.csv
sed -i "s/"$/' and org_id = '145079';/" customId.csv
source:
customId,_id
"005960","e14f6837-a66b-46a1-84c8-82b1c7e53fa9"
"006280","ce3f714b-c335-46ed-8481-5b5c15eaf5a3"
result:
update cloud_subscribers set subscriber_location_id='005960' where subscriber_id ='e14f6837-a66b-46a1-84c8-82b1c7e53fa9' and org_id = '145079';
update cloud_subscribers set subscriber_location_id='006280' where subscriber_id ='ce3f714b-c335-46ed-8481-5b5c15eaf5a3' and org_id = '145079';
#去除颜色字符
sed -i 's/x1b[[0-9;]*m//g' $log_file
postgres command:
#!/bin/bash export PGPASSWORD='xxx' pg_dump -h localhost -d cloud -s --exclude-table=cloud_subscriber_devices_0227,calixcalldisposition_backup,cloud_subscriber_devices_0227,cloud_subscribers_0227,david_billing,dv2,sxacc_devices_backup,sxaimsubscriber_next_endpoint_id_bak,sxaimsubscriber_next_endpoint_id_old,tblsizestats,csc_site_scan_results_* -U calixcloud -f schma.sql #pg_dump -h localhost -d cloud -s -F c #--exclude-table cloud_subscriber_devices_0227 calixcalldisposition_backup cloud_subscriber_devices_0227 cloud_subscribers_0227 david_billing dv2 sxacc_devices_backup sxaimsubscriber_next_endpoint_id_bak sxaimsubscriber_next_endpoint_id_old tblsizestats csc_site_scan_results_* #-U calixcloud -f schma.sql
打印颜色
RED='