shell 批量处理 字符串指令行
程序员文章站
2022-05-27 21:46:24
...
废话不说,直接看代码
附件中只应参考 curl-test.txt文件 代码以博客文本为主。
#!/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文件 代码以博客文本为主。
上一篇: ASP设计常见问题及解答精要-1
下一篇: 怎么学习asp 给非专业的asp爱好者