无法使用 System.cmd 从 Elixir 中运行curl 命令

Ser*_*pia 0 curl command system elixir

我只是尝试使用 Elixir 运行特定格式的curl 命令。

$ curl -w "@config/curl-format.txt" -o /dev/null -s "http://wordpress.com/"
0.004, 0.017, 0.000, 0.017, 0.000, 0.029, 0.029
Run Code Online (Sandbox Code Playgroud)

直接从终端运行命令效果很好。


这就是我在 Elixir 中尝试做的事情:

args = ["-w config/curl-format.txt", "-o /dev/null", "-s", "http://wordpress.com"]
result = System.cmd("curl", args, [])
Run Code Online (Sandbox Code Playgroud)

但我得到:

{" config/curl-format.txt", 23}
Run Code Online (Sandbox Code Playgroud)

而且结果和上面不一样。

Dog*_*ert 5

您的System.cmd调用相当于(在 shell 语法中):

curl "-w config/curl-format.txt" "-o /dev/null" -s http://wordpress.com
Run Code Online (Sandbox Code Playgroud)

您需要传入-wconfig/curl-format.txt-o/dev/null作为不同的参数。你也错过@@config/curl-format.txt。这应该有效:

args = ["-w", "@config/curl-format.txt", "-o", "/dev/null", "-s", "http://wordpress.com"]
result = System.cmd("curl", args, [])
Run Code Online (Sandbox Code Playgroud)