一次开发中使用过的shell命令
1.时间计算
starttime=`date +'%Y-%m-%d %H:%M:%S'`; endtime=`date +'%Y-%m-%d %H:%M:%S'`; start_seconds=$(date --date="$starttime" +%s); end_seconds=$(date --date="$endtime" +%s); echo end_seconds-start_seconds
2.获取链接返回的状态码
checkurl='http://www.baidu.com' curl --connect-timeout 1 -s -w "%{http_code}" -o temp ${checkurl}
3.while...do,for循环;条件判断if;退出程序exit
while (( `curl --connect-timeout 1 -s -w "%{http_code}" -o temp ${checkurl}` != '200' )) do for i in $(seq 1 20); do echo 'sleep countdown to wait url is ok:'$((20-$i)); sleep 1; done;//倒计时 endtime=`date +'%Y-%m-%d %H:%M:%S'`; start_seconds=$(date --date="$starttime" +%s); end_seconds=$(date --date="$endtime" +%s); if [[ $((end_seconds-start_seconds)) > 600 ]]; then exit 1 fi
done
4.字符串截取和替换
checkurl=${line#*=} //截取变量line,等号右边的保留 checkurl=${checkurl/\$\{ESMHOST\}/sss} //用sss替换checkurl中的${ESMHOST}
5.文本文件的字符串替换
文件中所有的abc都替换为aaaaa
sed -i 's#abc#aaaaa#g' ./docker-compose.yml
6.模糊匹配
如果变量包含字符串'register.node.checkurl'
if [[ $line =~ 'register.node.checkurl' ]];then fi