shell脚本应用

1.调用微信对账单下载接口

#!/bin/sh
#商户可以通过该接口下载自2017年6月1日起的历史资金流水账单
batlog=$LOGPATH/logfile.log
if test $#  -lt  12
then
    echo "必须输入参数:文件路径+文件名+URL+公众账号ID+商户号+资金账单日期+资金账户类型+随机字符串+密码key+证书+证书秘钥+原进程id";
    return 1
fi

#原进程id
logPrefixPID=${12}
time=`date '+%Y-%m-%d %H:%M:%S'`
echo $logPrefixPID"["$time"]开始执行:ksh $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12}" >> $batlog
#文件路径
filepath=$1
#文件名称
filename=$2
#接口URL
url=$3
#公众账号ID
appid=$4
#商户号
mch_id=$5
#资金账单日期
bill_date=$6
#资金账户类型
account_type=$7
#随机字符串
nonce_str=$8
#md5key
md5key=$9
#证书
certFile=${10}
#证书秘钥
keyFile=${11}
#查询结果文件
queryResultFile=$filepath"/"$filename".rst"
#utf8格式文件
queryResultFileUTF=$queryResultFile".utf"
#gbk格式文件
queryResultFileGBK=$queryResultFile".gbk"

context=$( echo "account_type=$account_type&appid=$appid&bill_date=$bill_date&mch_id=$mch_id&nonce_str=$nonce_str" )
tmpfile=$$.context
echo -n $context > $tmpfile
echo -n "&key="$md5key >> $tmpfile
sign=$( openssl dgst -hmac $md5key -sha256 $tmpfile | awk '{print $2}' | tr 'a-z' 'A-Z' )
rm $tmpfile
postcontent="<xml>"
postcontent=$postcontent"<appid>"$appid"</appid>"
postcontent=$postcontent"<bill_date>"$bill_date"</bill_date>"
postcontent=$postcontent"<account_type>"$account_type"</account_type>"
postcontent=$postcontent"<mch_id>"$mch_id"</mch_id>"
postcontent=$postcontent"<nonce_str>"$nonce_str"</nonce_str>"
postcontent=$postcontent"<sign>"$sign"</sign>"
postcontent=$postcontent"</xml>"

#清空中间临时文件
if [ -e $queryResultFileUTF ]
then
    rm $queryResultFileUTF
fi
if [ -e $queryResultFileGBK ]
then
    rm $queryResultFileGBK
fi

time=`date '+%Y-%m-%d %H:%M:%S'`
echo $logPrefixPID"["$time"]开始执行:wget -d -O $queryResultFileUTF --tries=1 --timeout=2 --no-check-certificate --certificate-type=PEM --certificate=$certFile --private-key-type=PEM --private-key=$keyFile --post-data=$postcontent $url" >> $batlog
wget -d -O $queryResultFileUTF --tries=1 --timeout=2 --no-check-certificate --certificate-type=PEM --certificate=$certFile --private-key-type=PEM --private-key=$keyFile --post-data=$postcontent $url
if [ -e $queryResultFileUTF ]
then
    time=`date '+%Y-%m-%d %H:%M:%S'`
    echo $logPrefixPID"["$time"]将文件编码由UTF-8转换为GBK" >> $batlog
    iconv -c -f utf-8 -t gbk $queryResultFileUTF > $queryResultFileGBK
    if [ $( cat $queryResultFileUTF | grep "<xml>" | wc | awk '{print $1}' ) -ne 0 ]
    then
        time=`date '+%Y-%m-%d %H:%M:%S'`
        echo $logPrefixPID"["$time"]下载资金账单接口异常返回:"`cat $queryResultFileGBK` >> $batlog
        return 1
    else
        mv $queryResultFileGBK $filepath"/"$filename
        time=`date '+%Y-%m-%d %H:%M:%S'`
        echo $logPrefixPID"["$time"]下载资金账单接口下载文件成功" >> $batlog
    fi
else
    time=`date '+%Y-%m-%d %H:%M:%S'`
    echo $logPrefixPID"["$time"]没有生成"$queryResultFileUTF >> $batlog
    return 1
fi
time=`date '+%Y-%m-%d %H:%M:%S'`
echo $logPrefixPID"["$time"]下载微信对账文件"$filename"成功" >> $batlog
return 0

2.循环查询

#!/bin/sh
#微信查询企业付款接口
#ksh QueryEnterprisePaymentToWeChat.etc /nfsc/bis_ifront_vol1/tmpfile/batdata WXQYFKDZ_10020389_20180116.req WXQYFKDZ_10020389_20180116 https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo wx360638309cc59c4e 10020389 e2ja71DtK4g5zV8He2ja71DtK4g5zV8H /wls/otherapps/bisinternetbank/cert/apiclient_cert_10020389_prd.pem /wls/otherapps/bisinternetbank/cert/apiclient_key_10020389_prd.pem [进程28143]
batlog=$LOGPATH/batchdatasvr.log
if test $# -lt 10
then
  echo "必须输入参数:路径+流水号文件名+查询结果保存文件名+域名+APPID+商户号+签名密钥key+证书+证书密钥+进程号"
  return 1
fi
time=`date '+%Y-%m-%d %H:%M:%S'`
echo ${@:10}"["$time"]开始执行:ksh $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 ${@:10}" >> $batlog
filepath=$1
bkSerial_filename=$2
result_filename=$3
url=$4
appid=$5
mch_id=$6
md5key=$7
certFile=$8
keyFile=$9
logPrefixPID=${@:10}
bkSerialFile=$filepath"/"$bkSerial_filename
queryResultFile=$filepath"/"$result_filename
queryResultFile_UTF=$queryResultFile".utf"
queryResultFile_GBK=$queryResultFile".gbk"
if [ -e $queryResultFile ]
then
  rm $queryResultFile
fi
for LINE in `cat $bkSerialFile`
do
  nonce_str=$( date +%Y%M%d%s )
  partner_trade_no=$LINE
  context=$( echo -n "appid=$appid&mch_id=$mch_id&nonce_str=$nonce_str&partner_trade_no=$partner_trade_no" )
  tmpfile=$$.context
  echo -n $context > $tmpfile
  echo -n "&key="$md5key >> $tmpfile
  sign=$( md5sum $tmpfile | awk '{print $1}' | tr 'a-z' 'A-Z' )
  postcontent="<xml>"
  postcontent=$postcontent"<appid>"$appid"</appid>"
  postcontent=$postcontent"<mch_id>"$mch_id"</mch_id>"
  postcontent=$postcontent"<nonce_str>"$nonce_str"</nonce_str>"
  postcontent=$postcontent"<partner_trade_no>"$partner_trade_no"</partner_trade_no>"
  postcontent=$postcontent"<sign>"$sign"</sign>"
  postcontent=$postcontent"</xml>"
  time=`date '+%Y-%m-%d %H:%M:%S'`
  echo $logPrefixPID"["$time"]开始执行:wget -d -O $queryResultFile_UTF --tries=1 --timeout=2 --no-check-certificate --certificate-type=PEM --certificate=$certFile --private-key-type=PEM --private-key=$keyFile --post-data=$postcontent $url" >> $batlog
  wget -d -O $queryResultFile_UTF --tries=1 --timeout=2 --no-check-certificate --certificate-type=PEM --certificate=$certFile --private-key-type=PEM --private-key=$keyFile --post-data=$postcontent $url
  if [ -s $queryResultFile_UTF ]
  then
    iconv -c -f utf-8 -t gbk $queryResultFile_UTF > $queryResultFile_GBK
    rm $queryResultFile_UTF
    sub_value=$( cat $queryResultFile_GBK | awk -F "<return_code>" '{print $2}' | awk -F "</return_code>" '{print $1}' | tr -d '\n' )
    echo $sub_value > $tmpfile
    return_code=$( cat $tmpfile | awk -F "[" '{print $3}' | awk -F "]" '{print $1}' | tr -d '\n' )
    if [ $return_code = SUCCESS ]
    then
      sub_value=$( cat $queryResultFile_GBK | awk -F "<result_code>" '{print $2}' | awk -F "</result_code>" '{print $1}' | tr -d '\n' )
      echo $sub_value > $tmpfile
      result_code=$( cat $tmpfile | awk -F "[" '{print $3}' | awk -F "]" '{print $1}' | tr -d '\n' )
      if [ $result_code = SUCCESS ]
      then
        sub_value=$( cat $queryResultFile_GBK | awk -F "<partner_trade_no>" '{print $2}' | awk -F "</partner_trade_no>" '{print $1}' | tr -d '\n' )
        echo $sub_value > $tmpfile
        rsp_partner_trade_no=$( cat $tmpfile | awk -F "[" '{print $3}' | awk -F "]" '{print $1}' | tr -d '\n' )

        sub_value=$( cat $queryResultFile_GBK | awk -F "<mch_id>" '{print $2}' | awk -F "</mch_id>" '{print $1}' | tr -d '\n' )
        echo $sub_value > $tmpfile
        rsp_mch_id=$( cat $tmpfile | awk -F "[" '{print $3}' | awk -F "]" '{print $1}' | tr -d '\n' )

        sub_value=$( cat $queryResultFile_GBK | awk -F "<detail_id>" '{print $2}' | awk -F "</detail_id>" '{print $1}' | tr -d '\n' )
        echo $sub_value > $tmpfile
        detail_id=$( cat $tmpfile | awk -F "[" '{print $3}' | awk -F "]" '{print $1}' | tr -d '\n' )

        sub_value=$( cat $queryResultFile_GBK | awk -F "<status>" '{print $2}' | awk -F "</status>" '{print $1}' | tr -d '\n' )
        echo $sub_value > $tmpfile
        status=$( cat $tmpfile | awk -F "[" '{print $3}' | awk -F "]" '{print $1}' | tr -d '\n' )

        sub_value=$( cat $queryResultFile_GBK | awk -F "<reason>" '{print $2}' | awk -F "</reason>" '{print $1}' | tr -d '\n' )
        echo $sub_value > $tmpfile
        reason=$( cat $tmpfile | awk -F "[" '{print $3}' | awk -F "]" '{print $1}' | tr -d '\n' )

        sub_value=$( cat $queryResultFile_GBK | awk -F "<openid>" '{print $2}' | awk -F "</openid>" '{print $1}' | tr -d '\n' )
        echo $sub_value > $tmpfile
        openid=$( cat $tmpfile | awk -F "[" '{print $3}' | awk -F "]" '{print $1}' | tr -d '\n' )

        sub_value=$( cat $queryResultFile_GBK | awk -F "<transfer_name>" '{print $2}' | awk -F "</transfer_name>" '{print $1}' | tr -d '\n' )
        echo $sub_value > $tmpfile
        transfer_name=$( cat $tmpfile | awk -F "[" '{print $3}' | awk -F "]" '{print $1}' | tr -d '\n' )

        sub_value=$( cat $queryResultFile_GBK | awk -F "<payment_amount>" '{print $2}' | awk -F "</payment_amount>" '{print $1}' | tr -d '\n' )
        echo $sub_value > $tmpfile
        payment_amount=$( cat $tmpfile | awk -F "[" '{print $3}' | awk -F "]" '{print $1}' | tr -d '\n' )

        sub_value=$( cat $queryResultFile_GBK | awk -F "<transfer_time>" '{print $2}' | awk -F "</transfer_time>" '{print $1}' | tr -d '\n' )
        echo $sub_value > $tmpfile
        transfer_time=$( cat $tmpfile | awk -F "[" '{print $3}' | awk -F "]" '{print $1}' | tr -d '\n' )

        sub_value=$( cat $queryResultFile_GBK | awk -F "<desc>" '{print $2}' | awk -F "</desc>" '{print $1}' | tr -d '\n' )
        echo $sub_value > $tmpfile
        desc=$( cat $tmpfile | awk -F "[" '{print $3}' | awk -F "]" '{print $1}' | tr -d '\n' )

        if [ $status = SUCCESS ]
        then
          rspContent=$rsp_partner_trade_no"|"$rsp_mch_id"|"$detail_id"|"$status"|"$reason"|"$openid"|"$transfer_name"|"$payment_amount"|"$transfer_time"|"$desc
          echo -e $rspContent >> $queryResultFile
        else
          rspcontent=`cat $queryResultFile_GBK`
          time=`date '+%Y-%m-%d %H:%M:%S'`
          echo $logPrefixPID"["$time"]"$rspcontent >> $batlog
        fi
      else
        rspcontent=`cat $queryResultFile_GBK`
        time=`date '+%Y-%m-%d %H:%M:%S'`
        echo $logPrefixPID"["$time"]"$rspcontent >> $batlog
      fi
    else
      rspcontent=`cat $queryResultFile_GBK`
      time=`date '+%Y-%m-%d %H:%M:%S'`
      echo $logPrefixPID"["$time"]"$rspcontent >> $batlog
    fi
  else
    time=`date '+%Y-%m-%d %H:%M:%S'`
    echo $logPrefixPID"["$time"]订单号为"$partner_trade_no"查询交易记录失败!" >> $batlog
  fi
  rm -f $tmpfile
done
if [ -s $queryResultFile ]
then
  return 0
else
  return 1
fi

全部评论

相关推荐

北漂的牛马人:211佬,包进的,可能是系统问题
点赞 评论 收藏
分享
不要停下啊:大二打开牛客,你有机会开卷了,卷起来,去找课程学习,在牛客上看看大家面试笔试都需要会什么,岗位有什么需求就去学什么,努力的人就一定会有收获,这句话从来都经得起考验,像我现在大三了啥也不会,被迫强行考研,炼狱难度开局,啥也不会,找工作没希望了,考研有丝丝机会
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务