1、时间格式化
`date +%Y%m%d_%m%d`
2、使用grep判断是否存在目标字符串
final_status=$(echo ${final_res} | grep "true");
3、ssh连接,执行命令并获取命令返回值
res=$(ssh username@ip "curl 'https://wwww.baidu.com'")
4、单行调用python并获取返回值
trigger_res=$(python3 -c "print('Submit task success' in $res['message'])")
5、通过cat 文件,循环登陆SSH后台执行命令
cat cpubox_usage_rate_info.txt | while read line
do
ssh -n 155.125.15."${line:0:3}" "~/stress-ng -c 1 --cpu-method all --taskset ${line:3:2} --cpu-load ${line:5} > 1.log 2>&1 &"
done
6、shell数值计算
# todo 测试时长 默认8小时
echo "开始收集日志"
time_hour=8
# shellcheck disable=SC2004
time_span=$((${time_hour}*3600));
7、awk 处理文本
second_load=$(echo ${load_info} | awk -F ' ' '{print $9}' | awk -F "," '{print$1}')
8、try catch
scp ${host_ip}:.config/log/text.txt ${pub_folder}/${curday}/health_log/log.txt || echo "下载日志失败"
9、获取命令执行完之后输入q才释放终端的场景
status_cmd=$(ssh ${host_ip} "git log");