I have a bash function to repeated curl call, accepting different URLs as function args. The urls are different and one of them has previous query, like "?foo=bar". The API behind these urls are defined by users but they meet the same standard and need a mandatory arg like client=xxx
. So I am thinking how to do this in bash scripting.
Now I am doing:
client_query=client=xxx
oauth_url_with_client=$(echo $oauth_url | sed 's/?/?'$client_query'&/g') # if found, replace first query with client query
echo "First step: $oauth_url_with_client"
# when there is no "?", no replace happens, need to append
if [[ $oauth_url_with_client != *"$client_query"* ]]; then
oauth_url_with_client=$oauth_url_with_client?$client_query
fi
echo "Final oauth url: $oauth_url_with_client"
But now I see:
First step: https://host/oauth2/token?client=xxx?foo=bar
Final oauth url: https://host/oauth2/token?client=xxx?foo=bar
I see sed
is not replacing ?
with ?client=xxx&
. What is the problem here?
Is there some good way to combine possible/optional query from URL with mandatory query in --data
or --form
? Not sure where to put these