此处脚本全部来源于arthas启动脚本.
基本常识
在 bash shell 中,$( ) 与` ` (反引号) 都是用来做命令替换用(commandsubstitution)的。$() <===>``eg: $(uname -s) <===> `uname -s` ==> 获取操作系统${}用于变量替换。一般情况下PORT=8080echo ${PORT}$0获取当前脚本名称
字符串中间存在变量
参考
${COOKIE}处
#!/bin/bash#author chenhsun00#上传excel到sso#0、确定传递的参数,是否可以正确的接受#1、下载数据#2、生成excel#3、上传COOKIE=$1echo "${COOKIE}"curl 'http://data.aaaa.com/ReportServer?op=fs_set&cmd=auth_getallreports' \-H 'Connection: keep-alive' \-H 'Cache-Control: max-age=0' \-H 'DNT: 1' \-H 'Upgrade-Insecure-Requests: 1' \-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' \-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' \-H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7' \-H 'Cookie: '"${COOKIE}"'' \--compressed \--insecure | iconv -f gbk -t utf8 > result.json
命令判断
ps -p 410 -o pid= > /dev/null 2>&1; echo $?#0 是成功 1是失败,失败的原因看具体的情况进行描述处理#2>&1 错误重定向到输入上边.
参数解析
#!/usr/bin/env bashusage(){echo 'ip or port is empty'}IP=""PORT=""while [[ $# -gt 0 ]]dokey="$1"case ${key} in-h|--help)usageexit 0;;--ip)IP="$2"shift # past argumentshift # past value;;--port)PORT="$2"shift # past argumentshift # past value;;*)echo unknown argsshift # past argumentesacdoneif [[ -z ${IP} ]]; thenusageexit 0fiif [[ -z ${PORT} ]]; thenusageexit 0fiecho ${IP} ${PORT}
date
date "+%F %T" #输出 2020-02-02 19:12:12
判断字符串
# -z 判断字符串是否为空(空=true) -n 判断字符串是否为非空(非空=true) xx==xx 2个字符串相等 !=不相等#!/usr/bin/env bashstr="xxx"if [[ -z ${str} ]]; thenecho what1elseecho what2fi#str="" 输出what1#str="xx" 输出what2
判断文件
# -e 判断文件是否存在 -d 判断目录是否存在# -r 判断文件是否存在并且可读 -w 存在并且可写 -x 存在并且可执行 加 ! 为没有[ -e /data/project/config.json ] && echo config.json || bad #判断是不是文件[-d /data/project ] && echo dir exist || dir not exist#!/usr/bin/env bashif [[ -e /data/project/config.json ]] ; thenecho 'good'elseecho 'bad'fi
PID 是否存在
pid=`ps -ef|grep BootMybatisApplication|grep -v grep |grep java |awk '{print $2}'`# target process id to attachif [ -z $pid ]thenecho target java process is missingexit -1elseecho start attach target process $pidfi
命令是否存在
[[ -x $(command -v jstack) ]] && echo 'command is exist' || echo 'command is missing'#${command -v jstack} output--->/usr/bin/jstack
BASH 函数
main(){echo $@}main 1 2 3
使用环境变量
echo $HOMEecho ${HOME}#script(脚本中用 ${HOME})# 判断权限[ ! -w "/root" ] && echo "permission denied, /root is not writable."check_permission(){[ ! -w "${HOME}" ] && exit_on_err 1 "permission denied, ${HOME} is not writable."}exit_on_err(){[[ ! -z "${2}" ]] && echo "${2}" 1>&2exit ${1}}
获取Java命令
#!/bin/sh# jps.sh version 1.0.2# there might be multiple java processes, e.g. log-agentJPS_CMDS=($(ps aux | grep java | grep -v 'grep java' | awk '{print $11}' | sed -n 's/java$/jps/p'))# find the first executable jps commandJPS_CMD=""for jps in ${JPS_CMDS[@]}; doif [ -x $jps ]; thenJPS_CMD=$jpsbreakfidoneif [ "$JPS_CMD" == "" ]; thenecho "No Java Process Found on this Machine."exit 1elseresult=`$JPS_CMD -lmv | grep -v jps`if [ "$result" == "" ]; thenps 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" }'elseecho "$result"fifi
修改文件权限
用户 用户组chown -R nobody:nobody log.log
获取目录
##获取当前文件的为止realpath "$0"#获取目录dirname /xxx/xxx
获取IP
# 使用route获取默认网卡[root@iZbp11om21c05wzu8e4tx0Z ~]# route |grep default |awk '{print $8}'eth0[root@iZbp11om21c05wzu8e4tx0Z ~]# ip route |grep defaultdefault via 172.16.255.253 dev eth0#根据网卡获取IP 再cut一波ip addr show eth0|grep inet|awk '{print $2}' |cut -f 1 -d /
