常用Shell命令

  1. BASEDIR=$(cd $(dirname $) && pwd)
  2. cd $BASEDIR>/dev/null

选取两个文件中相同的部分:(Must sort first)

  1. comm - <(sort /tmp/gcs.calix.com-wifi-pm-per-radio_50|uniq) <(sort /tmp/gcs.calix.com-wifi-pm-per-radio_dupcheck_50|uniq)

CURL:

  1. curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":100}' http://localhost/test/adduser

变量处理:

  1. #根据变量值获取变量
  2. pass=`eval echo '$'psql_"${env}_pass"`
  3. cmd=`eval echo '$'psql_"$env"`

数值处理:

  1. #判断是否为数字
  2. if [ -n "$org" ] && [ "$org" -eq "$org" ] >/dev/null; then
  3. echo "$org need process"
  4. cat result.txt|grep $org>_result_${org}
  5. fi
  6.  
  7. #数字累加
    step=`expr "$step" + 1`

输入参数:

  1. usage="Usage: $0 -o/--org orgId[Must] -p/--prepare t[Option] -f/--force t[Option] -d/--dry t[Option] -k/--kafka t[Option]"
  2. while [ "$1" != "" ]
  3. do
  4. case $ in
  5. -o|--org) shift
  6. orgId=$
  7. ;;
  8. -f|--force) shift
  9. if [ "$1" = "-p" ];then
  10. prepare=
  11. fi
  12. force=
  13. ;;
  14. -p|--prepare) shift
  15. if [ "$1" = "-f" ];then
  16. force=
  17. fi
  18. prepare=
  19. ;;
  20. -d|--dry) shift
  21. dry=
  22. ;;
  23. -k|--kafka) shift
  24. kafka_flg=$
  25. ;;
  26. *) echo $usage
  27. exit
  28. ;;
  29. esac
  30. shift
  31. done
  32. if [ -z $orgId ];then
  33. echo -e "$RED[Error] Missing orgId!$NC\r\n$usage"
  34. exit
  35. fi

确认函数:

  1. check_and_commit()
  2. {
  3. cmd=""
  4. step=`expr "$step" + `
  5. echo ""|tee -a $logs
  6. echo -e "$BLUE[`date +'%Y-%m-%d %H:%M:%S'`][Step $step] exec $1 $NC"|tee -a $log
  7. if [ $force -eq ];then
  8. while true; do
  9. read -p "Do you confirm to execute step $1? [y/n]" yn
  10. case $yn in
  11. [Yy]* )
  12. $;
  13. if [ $dry -eq ];then
  14. echo -e "$GREEN [Dry Run]$NC $cmd"|tee -a $log
  15. else
  16. echo $cmd|tee -a $log
  17. $cmd|tee $logsdir/$.log
  18. fi
  19. break;;
  20. [Nn]* ) echo "ignore step $1"|tee -a $log ;break;;
  21. esac
  22. done
  23. else
  24. $
  25. if [ $dry -eq ];then
  26. echo -e "$GREEN [Dry Run]$NC $cmd"|tee -a $log
  27. else
  28. echo $cmd|tee -a $log
  29. $cmd|tee -a $logsdir/$.log
  30. fi
  31. fi
  32. }
  1. prepare_message_confirm()
  2. {
  3. echo "Please make sure next items be done"
  4. echo -e "${RED} 1.env.sh use correct environment information ${NC}"
  5. echo -e "${RED} 2.all gcs vm had added the onecloud replay URL and restarted${NC}"
  6. echo -e "${RED} 3.make sure this vm can connect to brown field mongo/redshift/CMC gateway ${NC}"
  7. echo -e "${RED} 4.had startup cloud-subscriber with correct version and expose port 3424 ${NC}"
  8. echo -e "${RED} 5.brown field subscrbier-sync pod had patched ${NC}"
  9. if [ $force -eq ];then
  10. while true; do
  11. read -p "Do you confirm ? [y/n]" yn
  12. case $yn in
  13. [Yy]* ) echo "will continue to execute for org :$orgId";break;;
  14. [Nn]* ) exit - ;break;;
  15. esac
  16. done
  17. fi
  18.  
  19. }

时间函数:

  1. #根据时区显示时间:
    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}"

统计网络连接状态:

  1. #netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"\t",state[key]}'

  ESTABLISHED 21
  FIN_WAIT1 3
  SYN_SENT 1

  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

数组:

  1. IFS=',' read -ra org_array <<< "$orgs"
  2. for orgId in "${org_array[@]}"
  3. do

While 循环

  1. for s in `ls _result_*`
  2. do
  3. while [ `ps -ef|grep pm_missing_data_move.py|grep -v grep|wc -l` -gt ]
  4. do
  5. sleep 1s
  6. done
  7. if [ -f $s ];then
  8. if [ `ps -ef|grep -v grep|grep $s|wc -l` -eq ];then
  9. mv $s run_$s
  10. nohup python pm_missing_data_move.py -e tony.ben@calix.com --password FA200417# --filename run_$s && mv run_$s done_${s} &
  11. fi
  12. fi
  13. done

sed函数

  1. #普通替换
  2. sed -i "s/ENV/$env/g" $sql
  3.  
  4. #读取行
  5. sed -n '1,3p' xxx.txt //读取第一到3行
  6.  
  7. #删除
  8. 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:

  1. #!/bin/bash
  2. export PGPASSWORD='xxx'
  3. 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
  4. #pg_dump -h localhost -d cloud -s -F c \
  5. #--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_* \
  6. #-U calixcloud -f schma.sql

打印颜色

  1. RED='\033[0;31m'
  2. BLUE='\033[1;32m'
  3. GREEN='\033[1;34m'
  4. NC='\033[0m' # No Color
  5.  
  6. echo -e "$RED[Error] Missing orgId!$NC\r\n$usage"

mongo

  1. compare_mongo_postgres()
  2. {
  3. source_sxa_mongo=`mongo $source_sxa_mongo --eval "db.isMaster()['primary']"|grep |awk -F ':' '{print $1}'`
  4. export PGPASSWORD=$onecloud_postgres_password
  5. flg=
  6. for c in sxacc-devices sxacc-provisioning-records
  7. do
  8. mnum=`mongo $source_sxa_mongo/sxa --eval "db.getCollection('$c').find({'orgId':'$orgId'}).count()"|awk 'END {print}'`
  9. rm -rf check_collection_num.sql
  10. cp check_collection_num.sql.tmp check_collection_num.sql
  11. sed -i "s/TABLE/$c/g" check_collection_num.sql
  12. sed -i "s/ORGID/$orgId/g" check_collection_num.sql
  13. pnum=`psql -h $onecloud_postgres_host -d $onecloud_postgres_db -U $onecloud_postgres_username -f check_collection_num.sql|sed -n '3p'`
  14.  
  15. echo "$c mongo: $mnum postgres: $pnum"
  16. if [ $mnum -ne $pnum ];then
  17. flg=
  18. echo -e "$RED[Error] c: $c org: $orgId not match, Mongo: $mnum Post: $pnum $NC\r\n"
  19. fi
  20. done
  21. if [ $flg -eq ];then
  22. exit
  23. fi
  24.  
  25. }
  26.  
  27. #选取master节点
  28. target_sxa_mongo=`mongo $target_sxa_mongo --eval "db.isMaster()['primary']"|grep |awk -F ':' '{print $1}'`
  29.  
  30. #export导出
  31. mongoexport --host $source_sxa_mongo --port $source_sxa_mongo_port -d $source_sxa_mongo_db
  32. #import导入
  33. mongoimport --host $target_sxa_mongo --port $target_sxa_mongo_port -d $target_sxa_mongo_db

定制登录欢迎消息

  1. cd /etc/update-motd.d/
  2. cat <<EOF >-custom
  3. #!/bin/sh
  4. echo ""
  5. echo "\033[1;32m----------------------------------------------------\033[0m"
  6. echo "\033[0;31mTony.Ben's ACS Develop Server.\nAny questions please connect with tony.ben@calix.com\033[0m"
  7. echo "\033[1;32m----------------------------------------------------\033[0m"
  8. echo "\033[1;32m+++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m"
  9. echo "\033[0;31mLast Login Info\033[0m"
  10. last
  11. echo "\033[1;32m+++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m"
  12. echo ""
  13. echo "\033[1;32m-----------------------------------------------------\033[0m"
  14. ifconfig -a | awk 'BEGIN {FS="\n"; RS=""} {print $1,$2}' | grep -v 'lo' | awk '{print "\t\t"$1,$7}'
  15. echo "\033[1;32m------------------------------------------------------\033[0m"
  16. EOF
    chmod 751 100-custom

EOF

  1. EOFEND Of File的缩写,表示自定义终止符.既然自定义,那么EOF就不是固定的,可以随意设置别名,在linuxctrl-d就代表EOF.
  2.  
  3. EOF一般会配合cat能够多行文本输出.
  4.  
  5. 通过cat配合重定向能够生成文件并追加操作,在它之前先熟悉几个特殊符号:
  6. < :输入重定向
  7. > :输出重定向
  8. >> :输出重定向,进行追加,不会覆盖之前内容
  9. << :标准输入来自命令行的一对分隔号的中间内容.
  10.  
  11. 其用法如下:
  12. <<EOF //开始
  13. ....
  14. EOF //结束
  15.  
  16. 还可以自定义,比如自定义:
  17. <<BBB //开始
  18. ....
  19. BBB //结束
  20.  
  21. )向文件test.sh里输入内容。
  22. [root@slave-server opt]# cat << EOF >test.sh
  23. >
  24. >
  25. > asdfasdfs
  26. > EOF
  27.  
  28. [root@slave-server opt]# cat test.sh
  29.  
  30. asdfasdfs
  31.  
  32. 追加内容
  33. [root@slave-server opt]# cat << EOF >>test.sh
  34. >
  35. >
  36. > EOF
  37. [root@slave-server opt]# cat test.sh
  38.  
  39. asdfasdfs
  40.  
  41. 覆盖
  42. [root@slave-server opt]# cat << EOF >test.sh
  43. >
  44. > EOF
  45. [root@slave-server opt]# cat test.sh
  46.  
  47. ————————————————
  48. 版权声明:本文为CSDN博主「jaryle」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
  49. 原文链接:https://blog.csdn.net/jaryle/java/article/details/77880500

Rancher/K8S 相关

全量清除Rancher/K8S 节点

  1. docker stop $(docker ps -aq)
  2. docker system prune -f
  3. docker volume rm $(docker volume ls -q)
  4. docker image rm $(docker image ls -q)
  5. rm -rf /etc/ceph \
  6. /etc/cni \
  7. /etc/kubernetes \
  8. /opt/cni \
  9. /opt/rke \
  10. /run/secrets/kubernetes.io \
  11. /run/calico \
  12. /run/flannel \
  13. /var/lib/calico \
  14. /var/lib/etcd \
  15. /var/lib/cni \
  16. /var/lib/kubelet \
  17. /var/lib/rancher/rke/log \
  18. /var/log/containers \
  19. /var/log/pods \
  20. /var/run/calico

获取Rancher dockerid

  1. docker ps | grep -E "rancher/rancher:|rancher/rancher " | awk '{ print $1 }'

启动Rancher

  1. sudo docker run -d --name rancher --restart=unless-stopped -p : -p : rancher/rancher:latest

获取dockerId

  1. docker ps -aq

1.awk command

1.1 Purpose 1: want to distinct and then count and sort by num

1.1.1 Command: cat result.txt|awk '{print $1}'|uniq -c|sort -k 1n

  1. Sort parameters:

 -k: sort by key (in this case column, pairs with -t)

  -n: sort as a number

  -r: reverse order

  (optional) -t: in case you want to change the key separator (default: space)

Uniq parameter:

  -w: choose the first N characters

Explanation:

  In your problem, we need to first sort the first column and then the second one. So there is a -k 1,1 followed by -k 2,2. But, the second key (ONLY) must be sorted as a number and in the reverse order. Thus, it should be -k 2nr,2.

Note that if the -n or -r sort parameters are outside -k parameter, they are applied to the whole input instead of specific keys.

Lastly, me must find the unique lines, but matching only the first 4 chars. Thus, uniq -w 4

2. Ubuntu 安装ping curl

  1. apt-get update
  2. apt-get install iputils-ping -y
  3. apt-get install curl -y

comm shell command的更多相关文章

  1. ionic打包apkFailed to execute shell command "input,keyevent,82"" on device: Error: adb: Command failed with exit code 137

    错误代码如下 BUILD SUCCESSFUL in 12s 46 actionable tasks: 1 executed, 45 up-to-date Built the following ap ...

  2. Jenkins--Run shell command in jenkins as root user?

    You need to modify the permission for jenkins user so that you can run the shell commands. You can i ...

  3. I.MX6 Android shutdown shell command

    /******************************************************************************* * I.MX6 Android shu ...

  4. Makefile,Shell command,Shell Language 之间的联系

    1. Makefile 首先要知道Makefile 是什么东西,Makefile 是一个指令文件,里面存储着自定义的命令(可以借助已有的命令创造而来)在不同的系统下对Makefile 的区别不一样,L ...

  5. Jenkins可用环境变量列表以及环境变量的使用(Shell/Command/Maven/Ant)

    一.可用环境变量列表(以下来自google翻译): BRANCH_NAME 对于多分支项目,这将被设置为正在构建的分支的名称,例如,如果您希望从而master不是从特征分支部署到生产. CHANGE_ ...

  6. getopts shell command -options parameters

    说明:原文网址http://blog.chinaunix.net/uid-26807463-id-3151601.html 获取UNIX类型的选项: unix有一个优点就是标准UNIX命令在执行时都具 ...

  7. Android shell command execute Demo

    package com.android.utils; import java.io.File; import java.io.IOException; import java.io.InputStre ...

  8. Linux shell command学习笔记(二)

    <cut> 作用:从输入文件或者命令的输出中析取出各种域 用法:cut –c{字符串范围} –d{字段间分割符} –f{字段索引编号} 举例: (1)查看在线用户:who | cut –c ...

  9. Linux shell command学习笔记(一)

    Shell的种类有很多种,例如CSH,Bourne Shell,Korn Shell.在现在的大多数Linux发行版中,默认的Shell一般都是Bourne again shell(bash). &l ...

随机推荐

  1. 死锁造成oom的排错

    1.死锁的查看步骤 jps -l jstack xxxx(xxxx为java进程的进程号) ------ 2:查看java进程的参数: jps -l jinfo -flag printGcDetial ...

  2. [转帖]ORM框架的前世今生

    ORM框架的前世今生 https://www.cnblogs.com/7tiny/p/9551754.html 目录 一.ORM简介二.ORM的工作原理三.ORM的优缺点四.常见的ORM框架 一.OR ...

  3. springMVC设置不拦截静态资源的方法

    SpringMVC提供<mvc:resources>来设置静态资源,但是增加该设置如果采用通配符的方式增加拦截器的话仍然会被拦截器拦截,可采用如下方案进行解决: 方案一.拦截器中增加针对静 ...

  4. 嵌入式Linux学习笔记之第二阶段---文件I/O

    1.文件IO的四个函数 一些术语: 不带缓冲的I/O: 每个read和write都调用内核中的一个系统调用. 文件描述符: 一个非负整数,对内核而言,所以打开的文件都通过文件描述符引用. ①打开或创建 ...

  5. 文件类型分类:头文件dirent.h中定义的文件类型与linux内文件符号对应关系

    头文件 dirent.h 定义了文件类型: enum{    DT_UNKNOWN = 0,         //未知类型    DT_FIFO = 1,            //first in, ...

  6. Python开发【第一章】:简介和入门

    Python简介 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做为ABC 语言的一种继承. ...

  7. 使用Docker搭建svn服务器教程

    svn简介 SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subv ...

  8. beego入门笔记

    Beego Learn Note 示例环境在Deepin系统下. deepin 15.9.3 ├── Beego : 1.11.2 ├── GoVersion : go1.12.4 ├── GOOS ...

  9. Scratch编程与高中数学算法初步

    scratch编程与高中数学算法初步 一提到编程,大家可能觉得晦涩难懂,没有一定的英语和数学思维基础的人,一大串的编程代码让人望而步,何况是中小学生.   Scratch是一款由麻省理工学院(MIT) ...

  10. react实现设置答题器选项个数

    一,设置答题器选项import React, { useState, useEffect } from 'react' import PropTypes from 'prop-types' impor ...