I have a method where I'm executing a curl command and it returns the result. I would like to pass arguments to this method but it isn't working.

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

How can I use the $f parameter passed into the function inside the curl command?

asked Jun 1, 2018 at 13:37

Anthony's user avatar

AnthonyAnthony

36.1k49 gold badges181 silver badges288 bronze badges

0

You can use:

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"
}

and call it as:

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

answered Jun 1, 2018 at 13:41

anubhava's user avatar

anubhavaanubhava

787k67 gold badges597 silver badges665 bronze badges

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.