Home > OS >  Loop over a curl command throguh a text file using sed
Loop over a curl command throguh a text file using sed

Time:12-13

I have a curl commadn that will run through a GitHub runner, the file export.sh looks like:

while read -r auth0 &&  read -r roles <&3; do
    curl --request POST \
      --url 'https://YOUR_DOMAIN/api/v2/users/USER_ID/roles' \
      --header 'authorization: Bearer MGMT_API_ACCESS_TOKEN' \
      --header 'cache-control: no-cache' \
      --header 'content-type: application/json' \
      --data '{ "roles": [ "ROLE_ID" ] }'
done < final-user-id.txt 3<final-id.txt

I have a file final-user-id.txt, that is like this:

Jack
Amy
Colin

I also have a file final-id.txt:

role_1
role_2
role_3

I want to run export.sh in such a way that every time it picks up a new variable from final-user-id.txt and replace it with auth0 and "ROLE_ID" be replaced with the contents of final-id.txt So the fist output would be:

while read -r auth0 &&  read -r roles <&3; do
    curl --request POST \
      --url 'https://YOUR_DOMAIN/api/v2/users/Jack/roles' \
      --header 'authorization: Bearer MGMT_API_ACCESS_TOKEN' \
      --header 'cache-control: no-cache' \
      --header 'content-type: application/json' \
      --data '{ "roles": [ "role_1" ] }'
done < final-user-id.txt 3<final-id.txt

So on and so forth depending on the number of variables.

CodePudding user response:

How about something like this:

#!/usr/bin/env bash

while IFS= read -r auth0 && IFS= read -r roles <&3; do
  echo curl --request POST \
    --url "https://YOUR_DOMAIN/api/v2/users/$auth0/roles" \
    --header "authorization: Bearer MGMT_API_ACCESS_TOKEN" \
    --header "cache-control: no-cache" \
    --header "content-type: application/json" \
    --data "{ 'roles': [ '$roles'] }"
done < final-user-id.txt 3<final-id.txt

Output

curl --request POST --url https://YOUR_DOMAIN/api/v2/users/Jack/roles --header authorization: Bearer MGMT_API_ACCESS_TOKEN --header cache-control: no-cache --header content-type: application/json --data { 'roles': [ 'role_1'] }
curl --request POST --url https://YOUR_DOMAIN/api/v2/users/Amy/roles --header authorization: Bearer MGMT_API_ACCESS_TOKEN --header cache-control: no-cache --header content-type: application/json --data { 'roles': [ 'role_2'] }
curl --request POST --url https://YOUR_DOMAIN/api/v2/users/Colin/roles --header authorization: Bearer MGMT_API_ACCESS_TOKEN --header cache-control: no-cache --header content-type: application/json --data { 'roles': [ 'role_3'] }

  • Although I suggest to use a different fd for final-user-id.txt just i case curl is using/consuming stdin.

  • Remove the echo if you're satisfied with the output.


Since you're using bash as mentioned by @DennisWilliamson the builtin read command has the -u flag. so the first line could be written as:

while IFS= read -ru3 auth0 && IFS= read -ru4 roles ; do

And the last line should be written like:

done 3<final-user-id.txt 4<final-id.txt
  • Related