Search code examples
bashrhel

Adding double quotes around multiple variable string


I need to add quote marks around the curlstring variable outputs at the bottom in bold. Any advice?

NOTE: There are other functions to this code but i've tried to make it as simple as possible

#!/bin/bash

read -r -e -p "Would you like to get the access token? [Y/N]: " input

curlstring="curl"

read -r -e -p "Ignore cert errors? [Y/N]: " input
read -r -e -p "Would you like to add the HTTP verb?[Y/N]: " input
read -r -e -p "What is the Client ID? " clientid
read -r -e -p "What is the Grant Type? " granttype
read -r -e -p "What is the Client Secret? " clientsecret
read -r -e -p "What is the GUI username? " guiuser
read -r -e -p "And password of the user given above? " guipasswd
read -r -e -p "Whats the IP and Port number <ip:port>? " ipandport

curlstring=$curlstring" client_id="$clientid"&grant_type="$granttype"&client_secret="$clientsecret"&username="$guiuser"&password="$guipasswd" https://"$ipandport"/oauth/token"

echo "$curlstring"

My current output is

 curl -k -X POST -d client_id=stackoverflow&grant_type=testing&client_secret=372‌​ryc438t3948fj3u489f3‌​6&username=test&pass‌​word=testing 1.2.3.4:1111/oauth/token 

however I want output like this

curl -k -X POST -d "client_id=stackoverflow&grant_type="testing"&client_secret‌​=372ryc438t3948fj3u‌​489f36&username=te‌​st&password=testin‌​g" 1.2.3.4:1111/oauth/token 

Basically putting it in json format

Having changed the code to

read -r -e -p "Ignore cert errors? [Y/N]: " input
read -r -e -p "Would you like to add the HTTP verb?[Y/N]: " input
read -r -e -p "What is the Client ID? " clientid
read -r -e -p "What is the Grant Type? " granttype
read -r -e -p "What is the Client Secret? " clientsecret
read -r -e -p "What is the GUI username? " guiuser
read -r -e -p "And password of the user given above? " guipasswd
read -r -e -p "Whats the IP and Port number <ip:port>? " ipandport

curl=/usr/bin/curl
declare -p curlopt=()
curlopt+=( -k -X POST )
curlopt+=( -d "client_id='$clientid'" )
curlopt+=( -d "grant_type='$granttype'" )
curlopt+=( -d "client_secret='$clientsecret'" )
curlopt+=( -d "username='$guiuser'" )
curlopt+=( -d "password='$guipasswd'" )

$curl "${curlopt[@]}" "https://$ipandport/oauth/token"

This is now my output

+ curl=/usr/bin/curl
+ curlopt=()
+ declare -p curlopt
declare -a curlopt='()'
+ curlopt+=(-k -X POST)
+ curlopt+=(-d "client_id='$clientid'")
+ curlopt+=(-d "grant_type='$granttype'")
+ curlopt+=(-d "client_secret='$clientsecret'")
+ curlopt+=(-d "username='$guiuser'")
+ curlopt+=(-d "password='$guipasswd'")
+ /usr/bin/curl -k -X POST -d 'client_id='\''testing'\''' -d 'grant_type='\''testing'\''' -d 'client_secret='\''39f39834jf3m34'\''' -d 'username='\''test'\''' -d 'password='\''testing'\''' https://1.2.3.4:5678/oauth/token

Any idea how to get it to my expected output above?


Solution

  • Your question appears to include the line:

    curlstring=$curlstring" client_id="$clientid"&grant_type="$granttype"&client_secret="$clientsecret"&username="$guiuser"&password="$guipasswd" https://"$ipandport"/oauth/token"
    

    This is rather awkard, as you seem to be trying to expand your variables outside your double quotes. A better alternative would be to quote everything:

    curlstring="$curlstring 'client_id=$clientid&grant_type=$granttype&client_secret=$clientsecret&username=$guiuser&password=$guipasswd' https://$ipandport/oauth/token"
    

    Or even split the options into an array for easier management and reading:

    curl=/usr/bin/curl
    declare -a curlopt=()
    curlopt+=( -k -X POST )
    curlopt+=( -d "client_id='$clientid'" )
    curlopt+=( -d "grant_type='$granttype'" )
    curlopt+=( -d "client_secret='$clientsecret'" )
    curlopt+=( -d "username='$guiuser'" )
    curlopt+=( -d "password='$guipasswd'" )
    
    $curl "${curlopt[@]}" "https://$ipandport/oauth/token"
    

    Note that this suffers from a lack of input validation. What happens if the password contains a percent sign or ampersand?