此处脚本全部来源于arthas启动脚本.
基本常识
在 bash shell 中,$( ) 与` ` (反引号) 都是用来做命令替换用(commandsubstitution)的。
$() <===>``
eg: $(uname -s) <===> `uname -s` ==> 获取操作系统
${}用于变量替换。一般情况下
PORT=8080
echo ${PORT}
$0获取当前脚本名称
字符串中间存在变量
参考
${COOKIE}
处
#!/bin/bash
#author chenhsun00
#上传excel到sso
#0、确定传递的参数,是否可以正确的接受
#1、下载数据
#2、生成excel
#3、上传
COOKIE=$1
echo "${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 bash
usage(){
echo 'ip or port is empty'
}
IP=""
PORT=""
while [[ $# -gt 0 ]]
do
key="$1"
case ${key} in
-h|--help)
usage
exit 0
;;
--ip)
IP="$2"
shift # past argument
shift # past value
;;
--port)
PORT="$2"
shift # past argument
shift # past value
;;
*)
echo unknown args
shift # past argument
esac
done
if [[ -z ${IP} ]]; then
usage
exit 0
fi
if [[ -z ${PORT} ]]; then
usage
exit 0
fi
echo ${IP} ${PORT}
date
date "+%F %T" #输出 2020-02-02 19:12:12
判断字符串
# -z 判断字符串是否为空(空=true) -n 判断字符串是否为非空(非空=true) xx==xx 2个字符串相等 !=不相等
#!/usr/bin/env bash
str="xxx"
if [[ -z ${str} ]]; then
echo what1
else
echo what2
fi
#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 bash
if [[ -e /data/project/config.json ]] ; then
echo 'good'
else
echo 'bad'
fi
PID 是否存在
pid=`ps -ef|grep BootMybatisApplication|grep -v grep |grep java |awk '{print $2}'`
# target process id to attach
if [ -z $pid ]
then
echo target java process is missing
exit -1
else
echo start attach target process $pid
fi
命令是否存在
[[ -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 $HOME
echo ${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>&2
exit ${1}
}
获取Java命令
#!/bin/sh
# jps.sh version 1.0.2
# there might be multiple java processes, e.g. log-agent
JPS_CMDS=($(ps aux | grep java | grep -v 'grep java' | awk '{print $11}' | sed -n 's/java$/jps/p'))
# find the first executable jps command
JPS_CMD=""
for jps in ${JPS_CMDS[@]}; do
if [ -x $jps ]; then
JPS_CMD=$jps
break
fi
done
if [ "$JPS_CMD" == "" ]; then
echo "No Java Process Found on this Machine."
exit 1
else
result=`$JPS_CMD -lmv | grep -v jps`
if [ "$result" == "" ]; then
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" }'
else
echo "$result"
fi
fi
修改文件权限
用户 用户组
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 default
default via 172.16.255.253 dev eth0
#根据网卡获取IP 再cut一波
ip addr show eth0|grep inet|awk '{print $2}' |cut -f 1 -d /