此处脚本全部来源于arthas启动脚本.

基本常识

  1. bash shell 中,$( ) ` ` (反引号) 都是用来做命令替换用(commandsubstitution)的。
  2. $() <===>``
  3. eg: $(uname -s) <===> `uname -s` ==> 获取操作系统
  4. ${}用于变量替换。一般情况下
  5. PORT=8080
  6. echo ${PORT}
  7. $0获取当前脚本名称

字符串中间存在变量

参考 ${COOKIE}

  1. #!/bin/bash
  2. #author chenhsun00
  3. #上传excel到sso
  4. #0、确定传递的参数,是否可以正确的接受
  5. #1、下载数据
  6. #2、生成excel
  7. #3、上传
  8. COOKIE=$1
  9. echo "${COOKIE}"
  10. curl 'http://data.aaaa.com/ReportServer?op=fs_set&cmd=auth_getallreports' \
  11. -H 'Connection: keep-alive' \
  12. -H 'Cache-Control: max-age=0' \
  13. -H 'DNT: 1' \
  14. -H 'Upgrade-Insecure-Requests: 1' \
  15. -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36' \
  16. -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
  17. -H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7' \
  18. -H 'Cookie: '"${COOKIE}"'' \
  19. --compressed \
  20. --insecure | iconv -f gbk -t utf8 > result.json

命令判断

  1. ps -p 410 -o pid= > /dev/null 2>&1; echo $?
  2. #0 是成功 1是失败,失败的原因看具体的情况进行描述处理
  3. #2>&1 错误重定向到输入上边.

参数解析

  1. #!/usr/bin/env bash
  2. usage(){
  3. echo 'ip or port is empty'
  4. }
  5. IP=""
  6. PORT=""
  7. while [[ $# -gt 0 ]]
  8. do
  9. key="$1"
  10. case ${key} in
  11. -h|--help)
  12. usage
  13. exit 0
  14. ;;
  15. --ip)
  16. IP="$2"
  17. shift # past argument
  18. shift # past value
  19. ;;
  20. --port)
  21. PORT="$2"
  22. shift # past argument
  23. shift # past value
  24. ;;
  25. *)
  26. echo unknown args
  27. shift # past argument
  28. esac
  29. done
  30. if [[ -z ${IP} ]]; then
  31. usage
  32. exit 0
  33. fi
  34. if [[ -z ${PORT} ]]; then
  35. usage
  36. exit 0
  37. fi
  38. echo ${IP} ${PORT}

date

  1. date "+%F %T" #输出 2020-02-02 19:12:12

判断字符串

  1. # -z 判断字符串是否为空(空=true) -n 判断字符串是否为非空(非空=true) xx==xx 2个字符串相等 !=不相等
  2. #!/usr/bin/env bash
  3. str="xxx"
  4. if [[ -z ${str} ]]; then
  5. echo what1
  6. else
  7. echo what2
  8. fi
  9. #str="" 输出what1
  10. #str="xx" 输出what2

判断文件

  1. # -e 判断文件是否存在 -d 判断目录是否存在
  2. # -r 判断文件是否存在并且可读 -w 存在并且可写 -x 存在并且可执行 加 ! 为没有
  3. [ -e /data/project/config.json ] && echo config.json || bad #判断是不是文件
  4. [-d /data/project ] && echo dir exist || dir not exist
  5. #!/usr/bin/env bash
  6. if [[ -e /data/project/config.json ]] ; then
  7. echo 'good'
  8. else
  9. echo 'bad'
  10. fi

PID 是否存在

  1. pid=`ps -ef|grep BootMybatisApplication|grep -v grep |grep java |awk '{print $2}'`
  2. # target process id to attach
  3. if [ -z $pid ]
  4. then
  5. echo target java process is missing
  6. exit -1
  7. else
  8. echo start attach target process $pid
  9. fi

命令是否存在

  1. [[ -x $(command -v jstack) ]] && echo 'command is exist' || echo 'command is missing'
  2. #${command -v jstack} output--->/usr/bin/jstack

BASH 函数

  1. main(){
  2. echo $@
  3. }
  4. main 1 2 3

使用环境变量

  1. echo $HOME
  2. echo ${HOME}
  3. #script(脚本中用 ${HOME})
  4. # 判断权限
  5. [ ! -w "/root" ] && echo "permission denied, /root is not writable."
  6. check_permission()
  7. {
  8. [ ! -w "${HOME}" ] && exit_on_err 1 "permission denied, ${HOME} is not writable."
  9. }
  10. exit_on_err()
  11. {
  12. [[ ! -z "${2}" ]] && echo "${2}" 1>&2
  13. exit ${1}
  14. }

获取Java命令

  1. #!/bin/sh
  2. # jps.sh version 1.0.2
  3. # there might be multiple java processes, e.g. log-agent
  4. JPS_CMDS=($(ps aux | grep java | grep -v 'grep java' | awk '{print $11}' | sed -n 's/java$/jps/p'))
  5. # find the first executable jps command
  6. JPS_CMD=""
  7. for jps in ${JPS_CMDS[@]}; do
  8. if [ -x $jps ]; then
  9. JPS_CMD=$jps
  10. break
  11. fi
  12. done
  13. if [ "$JPS_CMD" == "" ]; then
  14. echo "No Java Process Found on this Machine."
  15. exit 1
  16. else
  17. result=`$JPS_CMD -lmv | grep -v jps`
  18. if [ "$result" == "" ]; then
  19. ps aux | grep -E '^admin.*java.*' | grep -v grep | awk 'BEGIN{ORS=""}{print $2" ";for(j=NF;j>=12;j--){if(match($j, /^\-[a-zA-Z0-9]/)) {break;} } for(i=j+1;i<=NF;i++) {print $i" "} for(i=12;i<=j;i++) {print $i" "} print "\n" }'
  20. else
  21. echo "$result"
  22. fi
  23. fi

修改文件权限

  1. 用户 用户组
  2. chown -R nobody:nobody log.log

获取目录

  1. ##获取当前文件的为止
  2. realpath "$0"
  3. #获取目录
  4. dirname /xxx/xxx

获取IP

  1. # 使用route获取默认网卡
  2. [root@iZbp11om21c05wzu8e4tx0Z ~]# route |grep default |awk '{print $8}'
  3. eth0
  4. [root@iZbp11om21c05wzu8e4tx0Z ~]# ip route |grep default
  5. default via 172.16.255.253 dev eth0
  6. #根据网卡获取IP 再cut一波
  7. ip addr show eth0|grep inet|awk '{print $2}' |cut -f 1 -d /