1.变量
1.1 定义变量
1.1.1 普通变量
- 变量名=变量值:变量值必须是一个整体,中间没有特殊字符
- 变量名=‘变量值’:单引号中的内容,原样赋值
- 变量名=“变量值”:如果双引号里有其他变量,会把变量的结果进行拼接,然后赋值
习惯:数字不加引号,其他默认加双引号
1.1.2 命令变量
- 变量名=
命令
- 变量名=$(命令)
1.2 使用变量
- $变量名:非标准写法
- ${变量名}:在双引号中使用变量的值,如定义普通变量3
- “${变量名}":标准使用方式
1.3 只读变量
readonly变量名
1.4 删除变量
unset 变量名
2. 数组
- 定义数组:数组名=(值1 值2 … 值n)
- 给数组的元素赋值:数组名[索引]=值
- 获取元素:${数组名[下标]}
- 获取长度:${#数组名[*或@]}
3. 条件运算符
条件表达式必须写在[]中,且注意前后保留空格,同时$?可以获取上一条语句的执行结果
3.1 字符串运算符
运算符 | 说明 | 举例 |
---|---|---|
= | 检测两个字符串是否相等 | [ $a = $b ] |
!= | [ $a != $b ] | |
-z | 检测字符串长度是否为0,不为0返回true | [ -z $a ] |
-n | 检测字符串长度是否不为0,不为0返回true | [ -n $a ] |
$ | [ $a ] |
更多可参考http://tiswww.case.edu/php/chet/bash/bash.html#lbBW
3.2 关系运算符
只支持数字,不支持字符串
运算符 | 说明 |
---|---|
-eq | 相等 |
-ne | 不相等 |
-gt | 大于 |
-lt | 小于 |
-ge | 大于等于 |
-le | 小于等于 |
3.3 布尔运算符
运算符 | 说明 |
---|---|
! | 取反 |
-o | 或运算 |
-a | 与运算 |
3.4 逻辑运算符
必须写在双中括号中[[ ]],且具有短路功能
运算符 | 说明 |
---|---|
&& | 与运算 |
|| | 或运算 |
4. if语句
if [ 条件 ]
then
语句体
fi
if [ 条件 ]
then
语句体
else
语句体
fi
if [ 条件 ]
then
语句体
elif [ 条件 ]
语句体
else
语句体
fi
5. for语句
for 变量 in 范围
do
循环体
done
# eg:
for var in A B C D E F
do
echo "${var}"
done
6. while语句
while 条件
do
循环体
done
#eg.
a=1
while [ "${a}" -le 10 ]
do
echo "${a}"
((a++))
done
7. demo
start.sh
bashPath=/usr/local/app
trpcPath=/usr/local/application/bin
logFile=$bashPath/startScript.log
server=datatalk-webserver-0.0.1-SNAPSHOT.jar
projectLogPath=/usr/local/app/datatalk/logs
source /etc/profile
if [ ! -d $bashPath ];then
mkdir -p $bashPath
fi
if [ ! -d $projectLogPath ];then
mkdir -p $projectLogPath
fi
sudo -- sh -c -e "echo '9.146.161.37 api.tapd.oa.com apiv2.tapd.oa.com' >> /etc/hosts"
#rasp setup
hasOuternet=$(echo $SUMERU_SERVER | grep "outer_net")
if [[ $env == "prod" ]] && [[ $hasOuternet != "" ]]; then
echo 'use rasp agent'
cd /usr/share/java
sudo rm -f tencent.rasp-1.0.2.jar
sudo rm -f libmsg_queue.so
sudo rm -f tencent.rasp.jar
sudo wget https://mirrors.tencent.com/repository/generic/RASP_JAVA/v1.0.2/tencent.rasp-1.0.2.jar
sudo wget https://mirrors.tencent.com/repository/generic/RASP_JAVA/v1.0.2/libmsg_queue.so
sudo ln -s /usr/share/java/tencent.rasp-1.0.2.jar /usr/share/java/tencent.rasp.jar
raspAgent="-javaagent:/usr/share/java/tencent.rasp.jar"
fi
if [[ $env == "prod" ]]; then
apm_service_name=beacon-datatalk
elif [[ $env == "pre" ]]; then
apm_service_name=beacon-datatalk-gray
elif [[ $env == "public_prod" ]]; then
apm_service_name=beacon-datatalk-public_prod
elif [[ $env == "public_pre" ]]; then
apm_service_name=beacon-datatalk-public_pre
else
apm_service_name=beacon-datatalk-test
fi
echo "apm_service_name=${apm_service_name}">> $logFile
#apm setup
cd /usr/local/app/
rm -rf apm*
wget -O apm.tar.gz http://apm-75118.cos.sz.gfp.tencent-cloud.com/f02ee61e8cd34e579fca722cab7d5a75db/apm-agent-0.0.4-SNAPSHOT.tar.gz
if [ $? -eq 0 ]; then
tar -xzvf apm.tar.gz
echo "agent.service_name=${apm_service_name}" >> /usr/local/app/apm-agent/config/agent.config
echo "plugin.cgi.extract_expression_response=response" >> /usr/local/app/apm-agent/config/agent.config
echo "plugin.cgi.extract_expression_response_code=response.code" >> /usr/local/app/apm-agent/config/agent.config
echo "plugin.cgi.extract_expression_response_message=response.message" >> /usr/local/app/apm-agent/config/agent.config
# 设置0代表正确的 biz_code
echo "plugin.cgi.success_response_code=0" >> /usr/local/app/apm-agent/config/agent.config
# 过滤心跳
echo "agent.ignore_path=/" >> /usr/local/app/apm-agent/config/agent.config
if [[ $env != "prod" ]] || [[ $cgiRecord == "off" ]];then
echo "plugin.cgi.record_cgi_scene=false" >> /usr/local/app/apm-agent/config/agent.config
echo "plugin.cgi.record_cgi_scene=false" >>$logFile
fi
cp /usr/local/app/apm-agent/optional-plugins/apm-jdbc-commons-0.0.4-SNAPSHOT.jar /usr/local/app/apm-agent/plugins/
cp /usr/local/app/apm-agent/optional-plugins/apm-mysql-5.x-plugin-0.0.4-SNAPSHOT.jar /usr/local/app/apm-agent/plugins/
cp /usr/local/app/apm-agent/optional-plugins/apm-mysql-6.x-plugin-0.0.4-SNAPSHOT.jar /usr/local/app/apm-agent/plugins/
cp /usr/local/app/apm-agent/optional-plugins/apm-mysql-8.x-plugin-0.0.4-SNAPSHOT.jar /usr/local/app/apm-agent/plugins/
cp /usr/local/app/apm-agent/optional-plugins/apm-mysql-commons-0.0.4-SNAPSHOT.jar /usr/local/app/apm-agent/plugins/
else
echo "apm download failed"
fi
#混沌工程----------------start
if [ "${SUMERU_ENV}" = "formal" ]; then
export ChaosNamespace=Production
else
export ChaosNamespace=Development
fi
export ChaosService=chaos.trpc.${SUMERU_APP}.${SUMERU_SERVER}-${SUMERU_ENV}
echo "ChaosService: ${ChaosService}" >> $logFile
echo "ChaosNamespace: ${ChaosNamespace}" >> $logFile
# 判断agent是否存在
processName="chaos_agent"
processNum=`ps -ef | grep -w $processName | grep -v grep | wc -l`
echo "processNum: ${processNum}" >> $logFile
if [ 0 == $processNum ]
then
# 执行安装agent脚本
sudo -S su root -c "export chaos_port=$chaos_port ; cd /usr/local/ && curl http://chaos.oa.com/chaosblade/install | bash -s ${ChaosService}_${ChaosNamespace}"
fi
#混沌工程----------------end
sudo chmod 777 /etc/resolv.conf
echo > /etc/resolv.conf
echo "nameserver 10.165.87.54" > /etc/resolv.conf
echo "nameserver 10.165.86.15" >> /etc/resolv.conf
sudo chmod 644 /etc/resolv.conf
chmod a+x $trpcPath/$server
if [[ $env == "wechat" ]] || [[ $env == "wechattest" ]];then
nohup java $raspAgent -javaagent:/usr/local/app/apm-agent/apm-agent-latest.jar $app_jvm_params -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=1024M -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSCompactAtFullCollection -XX:ErrorFile=/usr/local/app/java_error.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/usr/local/app/ -Xloggc:/usr/local/app/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=4096k -jar $trpcPath/$server 2>&1 | tee $trpcPath/server.log >> $trpcPath/server.log 2>&1 &
else
nohup java -Dplugins.telemetry.tpstelemetry.metrics.ip=$HOST_IP -Dplugins.telemetry.tpstelemetry.metrics.port=$METRICS_PORT $raspAgent -javaagent:/usr/local/app/apm-agent/apm-agent-latest.jar $app_jvm_params -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=1024M -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSCompactAtFullCollection -XX:ErrorFile=/usr/local/app/java_error.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/usr/local/app/ -Xloggc:/usr/local/app/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=4096k -jar $trpcPath/$server 2>&1 | tee $trpcPath/server.log >> $trpcPath/server.log 2>&1 &
fi
sleep 3
num=`ps -ef |grep $trpcPath/$server |grep -v grep|wc -l`
echo "the num of process after start is $num">>$logFile
if [ $num -lt 1 ];then
echo "the process is not exit after start ">>$logFile
exit 1
fi
#探活检测
CHECK_URL="http://127.0.0.1:$main_port/server/example/hello"
CHECK_HTTP_CODE=401
HTTP_CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code}"\n" $CHECK_URL`
startProcessFail=10002
echo $HTTP_CODE
CHECK_COUNT=0
until [ ${HTTP_CODE} == $CHECK_HTTP_CODE ]
do
sleep 1s
HTTP_CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code}"\n" $CHECK_URL`
echo "check url http code on $CHECK_URL, http code $CHECK_HTTP_CODE excepted, now is $HTTP_CODE" >> $logFile
echo $HTTP_CODE
CHECK_COUNT=$((${CHECK_COUNT} + 1))
if [ $CHECK_COUNT -eq 200 ]; then
exit $startProcessFail
fi
done
exit 0
stop.sh
#!/bin/bash
bashPath=/usr/local/app
trpcPath=/usr/local/application/bin
logFile=$bashPath/startScript.log
server=datatalk-webserver-0.0.1-SNAPSHOT.jar
pidInfo=$(ps -ef | grep "$trpcPath/$server" | grep -v grep | awk '{print $2}')
for pid in $pidInfo;do
kill -9 $pid
done
#check process num
num=$(ps -ef | grep "$trpcPath/$server" | grep -v grep |wc -l)
if [ $num -gt 0 ];then
echo "`date` after stop process in force way the processNum is $num still bigger than 0">>$logFile
exit 1
fi
exit 0