【问题标题】:How to pass arguments to a curl command in a bash method [duplicate]如何在bash方法中将参数传递给curl命令[重复]
【发布时间】:2018-11-11 16:07:19
【问题描述】:

我有一个方法,我正在执行 curl 命令并返回结果。我想将参数传递给这个方法,但它不起作用。

commande_mine() {
    local MY_OUTPUT=$(curl -X POST \
  http://localhost:5000/myapp \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
        "filepath": "$1"
    }')
    echo $MY_OUTPUT
}

for f in "/Users/anthony/my files"/*
do 
    commande_mine $f >> test.txt
    break # break first time until everything works as expected
done

如何使用$f 参数传入curl 命令内部的函数?

【问题讨论】:

    标签: bash function curl


    【解决方案1】:

    你可以使用:

    commande_mine() {
      local MY_OUTPUT=$(curl -X POST \
      http://localhost:5000/myapp \
      -H 'cache-control: no-cache' \
      -H 'content-type: application/json' \
      -d '{
            "filepath": "'"$1"'"
        }')
        echo "$MY_OUTPUT"
    }
    

    并将其称为:

    for f in "/Users/anthony/my files"/*
    do 
        commande_mine "$f"
        break
    done > test.txt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 2018-02-13
      相关资源
      最近更新 更多