Shell function syntax usage record

1. Time formatting

`date +%Y%m%d_%m%d`

2. Use grep to determine whether the target string exists

final_status=$(echo ${final_res} | grep "true");

3. SSH connection, execute the command and obtain the command return value

res=$(ssh username@ip "curl 'https://wwww.baidu.com'")

4. Call python in a single line and get the return value

trigger_res=$(python3 -c "print('Submit task success' in $res['message'])")

5. Through the cat file, log in to the SSH background to execute commands in a loop 

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 numerical calculation

# todo 测试时长 默认8小时
echo "开始收集日志"
time_hour=8
# shellcheck disable=SC2004
time_span=$((${time_hour}*3600));

7. awk processes text

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. Get the scene where you enter q before releasing the terminal after the command is executed.

status_cmd=$(ssh  ${host_ip} "git log");

Guess you like

Origin blog.csdn.net/bruce_van/article/details/132793750