简体   繁体   中英

How to pass a double quoted value of a variable, as argument of the curl command, in a bash script?

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

.
.     < 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:

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 )

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

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 .

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.

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.

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:

'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?

Thanks in advance

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM