【问题标题】:Adding double quotes around multiple variable string在多个变量字符串周围添加双引号
【发布时间】:2017-04-10 15:16:39
【问题描述】:

我需要在底部的 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

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

【问题讨论】:

  • 请修正您的格式,并将您的问题简化为一个最小的示例,带有示例输入和所需的输出。
  • 完成 - 希望更容易理解
  • 从您的脚本中删除 ** 并执行 declare -p curlstring 以查看您得到的字符串。最好使用函数或bash数组来存储命令行。
  • 你需要像"outout"这样粗体的输出吗?
  • @anubhava 报错了,我想使用函数,但我还在学习 bash,所以现在想尽可能简单

标签: bash rhel


【解决方案1】:

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

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"

请注意,这会导致缺少输入验证。如果密码包含百分号或 & 号会怎样?

【讨论】:

  • 这抛出了一个错误并且在这个数组中创建的代码没有出现在最后?
  • 它抛出了什么错误?您使用的是什么 shell(或 bash 版本)?
【解决方案2】:

这个

"${curlopt[@]}"

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

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

"${curlopt[*]}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多