用 cURL 命令中的变量替换字符串

fir*_*ire 1 linux shell automation curl groupme

我正在尝试尽可能简单地自动化 GroupMe 机器人。从命令行发送消息的一种简单方法是使用以下命令:

curl -d '{"text" : "Your message here", "bot_id" : "this_is_a_secret_string"}' https://api.groupme.com/v3/bots/post
Run Code Online (Sandbox Code Playgroud)

在 Shell 脚本中,我想替换"Your message here"var,其中var被设置为来自不同命令的输出。这可能吗?

我用它替换了“您的消息”的事情不起作用:

var
$var
(var)
$(var)
{var}
${var}
Run Code Online (Sandbox Code Playgroud)

双引号 ("") 内的任何内容都被视为字符串,因此在这些方面没有做太多尝试。

jeb*_*rle 5

var不会被评估,因为这是瓦特/单引号。解决此问题的一种方法是将 3 个字符串粉碎在一起:

curl -d '{"text" : "'"$var"'Your message here", "bot_id" : "this_is_a_secret_string"}' https://api.groupme.com/v3/bots/post
Run Code Online (Sandbox Code Playgroud)
  • 字符串 1: '{"text" : "'
  • 字符串 2: "$var"
  • 字符串 3: 'Your message here", "bot_id" : "this_is_a_secret_string"}'

注意:这仅在内容var非常简单时才有效。扩展后的字符串仍必须是有效的 JSON 字符串。