在多个变量字符串周围添加双引号

时间:2017-04-10 15:16:39

标签: bash rhel

我需要在粗体底部的curlstring变量输出周围添加引号。有什么建议吗?

注意:此代码还有其他功能,但我试图让它尽可能简单

#!/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"

我当前的输出是

 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 

然而我想要这样的输出

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 

基本上把它放在json格式

将代码更改为

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"

现在是我的输出

+ 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

知道如何将它达到我上面的预期输出吗?

2 个答案:

答案 0 :(得分:0)

您的问题似乎包括以下行:

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

这是相当尴尬的,因为您似乎正在尝试将扩展到之外的双引号变量。更好的选择是引用一切:

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

甚至可以将选项拆分为数组,以便于管理和阅读:

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"

请注意,这会因缺少输入验证而受到影响。如果密码包含百分号或符号,会发生什么?

答案 1 :(得分:0)

"${curlopt[@]}"

生成一个字符串列表,每个数组元素一个字符串。

如果要将所有数组元素连接到一个字符串,请使用以下命令:

"${curlopt[*]}"