【发布时间】: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=372ryc438t3948fj3u489f36&username=test&password=testing 1.2.3.4:1111/oauth/token
但是我想要这样的输出
curl -k -X POST -d "client_id=stackoverflow&grant_type="testing"&client_secret=372ryc438t3948fj3u489f36&username=test&password=testing" 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,所以现在想尽可能简单