我遇到了这个问题吗?
我有一个使用占位符持有sql的变量:
echo $sql SELECT PX_PROMOTION_ID,PRIORITY,STATUS,EXCLSVE,TYPE,PERORDLMT,PERSHOPPERLMT,TOTALLMT,RSV_INT,PX_GROUP_ID,CAMPAIGN_ID,STOREENT_ID,VERSION,REVISION,EFFECTIVE,TRANSFER,CDrequired,EXPIRE,LASTUPDATEBY,TO_CHAR(LASTUPDATE,'YYYYMMDD HH24MMSS') AS LASTUPDATE,TO_CHAR(STARTDATE,'YYYYMMDD HH24MMSS') AS STARTDATE,TO_CHAR(ENDDATE,'YYYYMMDD HH24MMSS') AS ENDDATE,TO_CHAR(RSV_TIME,'YYYYMMDD HH24MMSS') AS RSV_TIME,RSV_REAL,TGTSALES,NAME,CODE,RSV_VCH,OPTCOUNTER FROM PX_PROMOTION WHERE LASTUPDATE BETWEEN (SELECT MAX(BATCHSTART) FROM XRPTEBATCHCONTROL) AND TIMESTAMP('$BATCH_END')
我有另一个变量保存值:
echo $BATCH_END 2012-11-14 17:06:13
我想用占位符替换值。我对Unix脚本不是特别好,但是我已经尝试过:
echo $sql | sed -e "s/'$BATCH_END/$BATCH_END/g"
但它仍然没有被替换…
谁能帮忙?我想替换占位符,并将最终的字符串保留到$ sql
我还需要知道如何将输出的值重新输入到变量中,例如,我试过:
sql=`echo "$sql" | echo "${sql//\$BATCH_END/$BATCH_END}"`
您的脚本中缺少单引号对的结尾。
原文链接:https://www.f2er.com/bash/387451.html更改自:
echo $sql | sed -e "s/'$BATCH_END/$BATCH_END/g"
至:
echo $sql | sed -e "s/\$BATCH_END/$BATCH_END/g"
更新 – 按照后续评论:
要将上述替换的结果保存回$ sql,请执行以下操作之一:
# Preferred way sql=$(echo $sql | sed -e "s/\$BATCH_END/$BATCH_END/g") # Old way sql=`echo $sql | sed -e "s/\$BATCH_END/$BATCH_END/g"`
这被称为command substitution.语法($(…)与反引号的机箱)有效,但首选可以让您进行嵌套。
首选方式:Herestring
这可能比您所关心的更高级,但以下列方式可以节省您不必使用echo的子进程:
sql=$(sed -e "s/\$BATCH_END/$BATCH_END/g" <<< $sql)