凡是传递给server端的文本数据,如果过长必须要截取,例如截取前480个字符
echo ${lines:0:480}
若有下载文件步骤,须判断文件是否下载成功
length1=${pkg1_file[@]}
if [ $length1 -eq 0 ]; then
echo "下发安装包失败"
exit 1
fi
若有解压安装包步骤,需判断是否解压成功
su - oracle -c "cd $pkg_path; unzip -o $pkg1_file"
if [ $? -ne 0 ]; then
echo "解压安装包出错"
echo "中断操作…"
exit 1
fi
shell输出内容拼装成json格式时,需要去除每次插入的字符串后面的换行
echo ',{"code":"add_results","alert": 1,"value":"'$error'","result": "ok"}'| tr -d ["\n"]>>$reports
如果需要保存error数据里的换行:
echo ',{"code":"add_results","alert": 1,"value":"'| tr -d ["\n"]>>$reports
echo $error>>$reports
echo '","result": "ok"}'| tr -d ["\n"]>>$reports
若需要将shell以json样式输出的文本转json格式字符串供python或server端使用,需要进行转换
import os
import os
import json
import sys
import socket
if not os.path.exists(reports) or reports in ('',None) :
print '巡检报告不存在'
sys.exit(1)
with open(reports,'r') as f:
lines=f.read().strip(',').strip('\n').replace('\n','\\n').replace('\t','\\t')
name=socket.gethostname()
results=[]
results.append(name)
wlines=[i for i in lines.split('},')]
for line in wlines[:-1]:
results.append(json.loads(line+'}'))
if len(wlines)==1:
#print wlines[0]
results.append(json.loads(wlines[0]))
else:
results.append(json.loads(wlines[-1]))
results=json.dumps(results)
#print results
由于server端下发的数值全部为字符串,因此进行与基值的数据比较时,可以先转换成字符串,本例中基值为db_use_base_value
typeset C=$(echo ${open_status} "$db_use_base_value" | awk '{print $1-$2}')
types=`echo ${C:0:1}`
if [ "$types" != "-" ]
then
error=$error$table_name"---"$open_status"%,"
fi
let "line_num=$line_num+1"
done
fi
除法取两位小数
use_size=$((total_size-free_size))
use_percent=$(echo "scale=2; $use_size*100/$total_size" | bc)