如何在bash脚本中传递变量的双引号值作为curl命令的参数?

时间:2021-10-26 15:42:01

I have a script on a server whose last line is an echo which prints the value of an URL something like this:

我在服务器上有一个脚本,其最后一行是一个echo,它打印一个URL的值,如下所示:

.
.     < the script code here >
.
echo "url"      # The last line of this script

I don't have the possibility of changing this script and I can just capture its return value, that is, capture the url as the return value inside a variable declared in my local bash script, something like this:

我没有更改此脚本的可能性,我可以捕获它的返回值,也就是说,将url捕获为在我的本地bash脚本中声明的变量内的返回值,如下所示:

cmd_url=$(ssh . . . <distinct script>)

Here is an example of such URL (for confidentiality reasons, of course I changed the values)

以下是此类网址的示例(出于保密原因,我当然更改了值)

http://mydomain:56789/cgi-bin/WebObjects/JavaMonitor.woa/admin/running?type=app&name=AppTest&pw=abcd&9e0Xy

The above example is principally a WebObjects application info URL, which can be passed as an argument of the curl command in order to know whether an application is running (so the application name in this example is AppTest and the JavaMonitor password that I have to provide in order to be able to query is abcd&9e0Xy)

上面的示例主要是一个WebObjects应用程序信息URL,它可以作为curl命令的参数传递,以便了解应用程序是否正在运行(因此,此示例中的应用程序名称是AppTest和我必须提供的JavaMonitor密码为了能够查询是abcd&9e0Xy)

If I open a terminal, I can simply paste this url enclosed within single quotes and run the following

如果我打开一个终端,我可以简单地将这个URL粘贴在单引号中并运行以下命令

curl -s -X GET 'http://mydomain:56789/cgi-bin/WebObjects/JavaMonitor.woa/admin/running?type=app&name=AppTest&pw=abcd9e0Xy'

So I have to enclose the string within single quotes to escape special characters and that works pretty well, if the application AppTest is actually running then curl returns YES.

因此,我必须将字符串括在单引号内以转义特殊字符并且效果很好,如果应用程序AppTest实际运行,则curl返回YES。

Now what I need to do, is to do exactly what I wrote above except that it is done inside a bash script and the url is not written manually enclosed within single quotes but it is stored inside a variable.

现在我需要做的就是完全按照上面所写的那样做,除了它是在一个bash脚本中完成的,并且url不是手动编写的单引号,而是存储在变量中。

Here is what I do:

这是我做的:

cmd_url=$(ssh . . . <distant script>)
ret_msg=$(curl -s -X GET "$cmd_url")

And the problem is (I presume) due to the special characters in the URL. It seems that curl fails to interpret the argument correctly. I even removed double quotes but that didn't solve the problem either.

由于URL中的特殊字符,问题是(我推测)。似乎curl无法正确解释这个论点。我甚至删除了双引号,但这也没有解决问题。

So my question is: how should I proceed ? How can I convert an already double quoted string (recall that this is done on the distant server & I cannot change that) to a single quoted string? So that curl would take it just as a sequence of characters without interpreting them? like what I wrote above in the terminal example:

所以我的问题是:我该怎么办?如何将已经双引号的字符串(回想一下,这是在远程服务器上完成并且我无法更改)转换为单引号字符串?那么curl会把它当作一系列字符而不解释它们吗?就像我在上面的终端示例中写的那样:

'http://mydomain:56789/cgi-bin/WebObjects/JavaMonitor.woa/admin/running?type=app&name=AppTest&pw=abcd9e0Xy'

So far I've tried different combinations of possible concatenation with single quotes but the problem persists. Either curl doesn't return anything at all or I get an error message indicating the password has not been provided.

到目前为止,我已经尝试了可能串联与单引号的不同组合,但问题仍然存在。卷曲不会返回任何内容,或者我收到一条错误消息,指出尚未提供密码。

Any idea? How can I pass an already double quoted local variable to curl in a bash script?

任何的想法?如何在bash脚本中传递已经双引号的局部变量?

Thanks in advance

提前致谢

1 个解决方案

#1


You could use tr to translate the double-quote to single-quote

您可以使用tr将双引号转换为单引号

$ echo \"foo\" | tr '"' "'"
'foo'

#1


You could use tr to translate the double-quote to single-quote

您可以使用tr将双引号转换为单引号

$ echo \"foo\" | tr '"' "'"
'foo'