废话不说,直接看代码
#!/bin/bash
#file_name="curl_test.txt"
#cur_dateTime="`date +%Y-%m-%d-%H-%m-%s`"
:<<!
@date 2018-03-12
@author Jeff.jing
@desc
使用规则:
1 脚本需要有执行权限 chmod 755
2 同目录需要有测试单元脚本 curl-test.txt
脚本初始化规则
1 测试脚本中 通用型替换符应被命名为 key_space_word
邮箱或mobile 应相应命名为 home_email_key_space/work_email_key_space/ mobile_key_space
如有新增 添加模板即可。
!
result_file="result.txt"
tmp_file='curl.tmp'
tmp_result_file='result.tmp'
tmp_uuid='tmp_uuid'
line_tmp='line_tmp'
source_file='curl-test.txt'
error_file='test-error.txt'
test_code='test_code'
error_count=0
total_count=0;
echo "ecf test betch hand is begin,please wait a second..."
#临时文件与目录处理
if [ ! -f $source_file ]; then
echo $source_file " is not found,please check it and try again later !"
exit 1
fi
if [[ ! -s $source_file ]];then
echo $source_file " is is in existence but nothing in it ,please check it and try again later !"
exit 1
fi
if [ -f $result_file ]; then
rm $result_file
fi
if [ -f $tmp_file ]; then
rm $tmp_file
fi
if [ -f $tmp_result_file ]; then
rm $tmp_result_file
fi
if [ -f $tmp_uuid ]; then
rm $tmp_uuid
fi
if [ -f $line_tmp ]; then
rm $line_tmp
fi
if [ -f $error_file ]; then
rm $error_file
fi
if [ -f $test_code ]; then
rm $test_code
fi
touch $result_file
touch $tmp_file
touch $tmp_uuid
touch $tmp_result_file
touch $line_tmp
touch $error_file
touch $test_code
#数组检查
function xrsh_arrhasitem()
{
local _xrsh_tmp
local ok_code=`echo "$1"`
for _xrsh_tmp in ${ok_code[*]}; do
if test "$2" = "$_xrsh_tmp"; then
echo yes
return
fi
done
echo no
}
function rand(){
min=$1
max=$(($2-$min+1))
num=$(($RANDOM+1000000000)) #增加一个10位的数再求余
echo $(($num%$max+$min))
}
function uuid_ran(){
cat /proc/sys/kernel/random/uuid>$tmp_uuid
uuid_ran=$(cat $tmp_uuid)
echo $uuid_ran
}
# 1 格式化处理原文件
sed '/^#.*\|^$/d' $source_file>$tmp_file
sed -i 's/\\/\\\\\\/g' $tmp_file
# 2 临时文件、目录更新
while read line
do
let total_count++
rnd=$(rand 400000 500000)
uuid_str=$(uuid_ran)
key_word='ecf-test-'${uuid_str:0:15}$rnd
#echo $key_word
echo -E $line > $line_tmp
#curl_test=${line/key_space_word/$key_word}
sed -i "s/key_space_word/$key_word/g" $line_tmp
#替换phone phone-> '+1050000'$rnd
phone='+1050000'$rnd
#curl_test=${curl_test/phone_key_space/$phone}
sed -i "s/mobile_key_space/$phone/g" $line_tmp
work_phone='+1050089'$rnd
#curl_test=${curl_test/phone_key_space/$phone}
sed -i "s/work_phone_key_space/$work_phone/g" $line_tmp
#替换邮箱 emial->key_word@eric.com
home_email=$key_word'@home.com'
#curl_test=${curl_test/email_key_space/$email}
sed -i "s/home_email_key_space/$home_email/g" $line_tmp
work_email=$(uuid_ran)'@work.com'
sed -i "s/work_email_key_space/$work_email/g" $line_tmp
eq='eq "'
sed -i "s/$eq/$work_email/g" $line_tmp
#cat $line_tmp
#uuid_ran=${uuid:0:14}
#key_space_word=$uuid_ran$rnd
#echo $key_space_word
#eval $line >> $tmp_result_file
shell_line=$(cat $line_tmp)
#echo " shell code is : "$shell_line
eval $shell_line > $tmp_result_file
#cat $tmp_result_file
tmp_result=$(cat $tmp_result_file)
echo "return result is : "$(head -1 $tmp_result_file)>> $result_file
echo "return schema is : " >> $result_file
echo $tmp_result >> $result_file
#换行符
echo >> $result_file
#cat 'curl result is : '$tmp_result_file
xrsh_tmp=`echo ${ok_code[*]}`
http_code=$(head -1 $tmp_result_file | awk '{print$2}')
#echo "http_code is "$http_code
if [ ! -n "$http_code" ] ;then
http_code=400
fi
status=$(xrsh_arrhasitem "$xrsh_tmp" $http_code)
echo $status > $test_code
#if test $(cat $test_code) -eq "yes" ; then
if [ $http_code -eq 200 -o $http_code -eq 201 -o $http_code -eq 100 ]
then
echo "test is ok !"
else
echo "some error has aappeanded and http_code is "$http_code" , so write it to one error file "
echo "http code is : "$http_code >>$error_file
echo "error request is : "$shell_line>>$error_file
echo $tmp_result >> $error_file
let error_count++
fi
done <$tmp_file
echo "There are altogether " $total_count" test has been executed, error test is " $error_count" ,check "$error_file " or "$result_file" for detail ."
rm $tmp_file
rm $tmp_result_file
rm $tmp_uuid
rm $line_tmp
rm $test_code
附件中只应参考 curl-test.txt文件 代码以博客文本为主。
分享到:
相关推荐
将需要替换的文件放到脚本同级目录,直接执行脚本即可。 根据脚本提示输入 替换前和替换后的字符串
shell中字符串截取shell中字符串截取shell中字符串截取shell中字符串截取shell中字符串截取shell中字符串截取shell中字符串截取shell中字符串截取shell中字符串截取shell中字符串截取shell中字符串截取shell中字符串...
在做shell批处理程序时候,经常会涉及到字符串相关操作。有很多命令语句,如:awk,sed都可以做字符串各种操作。 其实shell内置一系列操作符号,可以达到类似效果,大家知道,使用内部操作符会省略启动外部程序等时间...
该脚本可以批量替换字符串,将需要替换的文件放到脚本同级目录,直接执行脚本即可。 根据脚本提示输入 替换前和替换后的字符串
shell中字符串处理方式,Linux下灵活活处理字符串的说明。。
shell 字符串操作 关于字符的常用命令
shell 字符串 截取 查找 替换 shell 字符串 截取 查找 替换 shell 字符串 截取 查找 替换 shell 字符串 截取 查找 替换
本文阐述:shell中整数比较方法及字符串的比较方法,如等于,不等于,大于,大于等于,小于,等等
linux shell 字符串的截取
Shell 删除指定文件中含有指定字符的行,2个参数,脚本中有注释
Linux Shell字符串比较
第一、找出字符或者字符串的类型,是数字、字母还是其他特定字符,是可打印字符,还是不可打印字符(一些控制字符)。 第二、找出组成字符串的字符个数和字符串的存储结构(比如数组)。 第三、对串的常规操作:求子串、...
一、Linux shell 截取字符变量的前8位,有方法如下: 代码如下:1.expr substr “$a” 1 82.echo $a|awk ‘{print substr(,1,8)}’3.echo $a|cut -c1-84.echo $5.expr $a : ‘\(.\\).*’6.echo $a|dd bs=1 count=8 2>...
通过shell语言进行字符串的比较以及实现数值的比较
shell 操作字符串
以下给出一些shell中判断字符串包含的方法,来源程序员问答网站 stackoverflow 以及segmentfault。 方法一:利用grep查找 strA=long string strB=string result=$(echo $strA | grep ${strB}) if [[ $result != ]] ...
01 获取字符串所在的行数 方式一:用grep -n [root@root]# cat test apple bit create delect exe flow good [root@root]# cat test | grep -n exe 5:exe [root@root]# cat test | grep -n exe | awk -F : '{...
主要介绍了Shell脚本中计算字符串长度的5种方法,来自于个人Shell脚本长期的开发经验,需要的朋友可以参考下
1. 字符串声明 概述 字符串的基本操作 脚本 1 # 声明字符串 str01=str01 echo ${str01} # 单引号也可以 # 不过后面的例子, 通常是用 双引号, 具体原因, 以后会解释\nstr02='str02' echo ${str02} # 对引号的转义,...