写shell脚本时经常会拼接一个字串,然后当成命令执行,用到的linux命令是eval ${str}
或者echo ${str} | sh
,例子如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #!/usr/bin/env bash
start=1 amount=$1 raceId=$2 api_host=127.0.0.1:8080 str="curl --location --request POST 'http://${api_host}/api/AddBuy' --header \"'Content-Type':'application/json'\" --data-raw '{ \"PidStart\":$start, \"Amount\":${amount}, \"RaceId\":${raceId} }'"
echo ${str} | sh
eval ${str}
|