【问题标题】:How to use variable in curl command in Linux bash shell script to send post request with file?如何在 Linux bash shell 脚本中使用 curl 命令中的变量来发送带有文件的发布请求?
【发布时间】:2017-11-27 17:43:56
【问题描述】:

例如。 我可以在终端成功执行以下命令:

curl -sX POST -F 'file=@/tmp/test.tgz' -F 'fileName=test.tgz' http://localhost:8080/upload

但是,如果我将文件路径 str "/tmp/test.tgz" 替换为 bash shell 脚本中的变量,那么它将无法工作,请您帮忙告诉我如何解决它?

curl -sX POST -F 'file=@$myfile' -F 'fileName=test.tgz' http://localhost:8080/upload

【问题讨论】:

    标签: linux shell curl post upload


    【解决方案1】:

    如果你想要变量扩展,使用 " 而不是 '

    【讨论】:

      【解决方案2】:
      ff="'file=$myfile'"
      fname="'fileName=test.tgz'"
      eval curl -sX POST -F $ff $fname http://localhost:8080/upload
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-17
        • 2016-09-26
        • 2022-08-14
        • 2020-08-05
        • 1970-01-01
        • 2011-09-18
        • 2012-10-17
        相关资源
        最近更新 更多