如何在bash方法

时间:2018-06-01 13:37:17

标签: bash function curl

我有一个方法,我正在执行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参数?

1 个答案:

答案 0 :(得分: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