shell脚本中curl带参数的命令替换,双引号不需要转义,像写json一样写字符串,curl命令使用单引号引起来,执行时使用$()而不是反引号``,awk也是同样,使用单引号把命令引起来,双引号不需要转义,像写json一样直接写出来;
以下是具体的代码:
#!/bin/bash cd `dirname $0` CUR_DIR=`pwd` UID_FILE=$CUR_DIR/uid.txt if [ ! -e $UID_FILE ]; then echo "ERROR:uid.txt not found" exit 1 fi PHONE_PRE='curl -X POST -d q={"uid":"' PHONE_SUF='"} http://192.168.238.11:8080/user/getUserinfoByUid' NAME_PRE='curl -X POST -d driverId=' NAME_SUF='&productId=2148131421781&caller=am0131777124&mode={"needDriverBiz":1,"needDriverBasic":1}&fields=[name,photo_url] http://192.168.156.121:8080/driver/getDriver' for line in `cat $UID_FILE` do #statements echo "uid:$line" PHONE_COMMAND="${PHONE_PRE}${line}${PHONE_SUF}" echo "PHONE_COMMAND:$PHONE_COMMAND" PHONE_RES=$($PHONE_COMMAND) echo "PHONE_RES:$PHONE_RES" PHONE=`echo $PHONE_RES | awk '/cell/{a=index($0,"cell");if(a>1){c=substr($0,a+6,13);print c;}}'` NAME_COMMAND="${NAME_PRE}${line}${NAME_SUF}" echo "NAME_COMMAND:$NAME_COMMAND" NAME_RES=$($NAME_COMMAND) echo "NAME_RES:$NAME_RES" NAME_D=`echo $NAME_RES | awk '/driver_name/{a=index($0,"driver_name");if(a>5){c=substr($0,a+13,23);print c;}}'` echo $line $PHONE $NAME_D >> uid_phone_name.txt done