LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash/Shell: Problem using a variable in a script (http://www.linuxquestions.org/questions/linux-general-1/bash-shell-problem-using-a-variable-in-a-script-664129/)

redvivi 08-20-2008 02:06 PM

Bash/Shell: Problem using a variable in a script
 
Hello !

I have a problem using this variable:

Code:

HEADERS="-H 'Accept: text/xml' \
-H 'Accept:Encoding: gzip'"

If I run this command:

Code:

curl $HEADERS foo.com
curl considers the value of $HEADERS as an URL, which, obviously, leads to an error

What can I do to use properly this variable as a parameters ?

Thanks!
RedVivi

CRC123 08-20-2008 03:06 PM

Hello, try this:

Code:

HEADERS="-H \'Accept: text/xml\' \
-H \'Accept:Encoding: gzip\'"

Your code would work if directly on the command line, but since your storing it in a variable, the single quotes are removed. If you were to look at the HEADERS variable after your assignment to it, there would be no single quotes. For this reason, the program is parsing the options wrong since there are no single quotes there to tell it to keep going after it hits a space. You could also try not putting spaces between any colons in your variable.

Anyway, to keep the single quotes there, you need to escape them with the backslash(\). This way, when the shell expresses your variable, it will include the single quotes with it.

Good luck

jschiwal 08-20-2008 03:17 PM

Reference the variables in double quotes: "${HEADERS}".
Also look at the man page for curl.

Code:

              curl will make sure that each header you  add/replace  get  sent
              with the proper end of line marker, you should thus not add that
              as a part of the header content: do not add newlines or carriage
              returns they will only mess things up for you.


redvivi 08-20-2008 03:20 PM

Quote:

Originally Posted by jschiwal (Post 3254131)
Include the variables in double quotes: "${HEADERS}".
Also look at the man page for curl.

Code:

              curl will make sure that each header you  add/replace  get  sent
              with the proper end of line marker, you should thus not add that
              as a part of the header content: do not add newlines or carriage
              returns they will only mess things up for you.


I have no problems with curl, "/" is interpreted by the shell when assigning the variable IMO but not in the command itself. Moreover, "/" isn't a part of the headers, it's just between the parameters of each headers.

redvivi 08-21-2008 08:55 AM

I tried the solutions you adviced me. Unfortunately, it doesn't work. I tried several syntaxes to try to use the $HEADERS variable, please have a look at these lines:

Code:

~ user$ HEADERS="-H 'Accept: text/xml' -H 'Accept:Encoding: gzip'"
~ user$ curl $HEADERS google.de
curl: (6) Couldn't resolve host 'text'
curl: (6) Couldn't resolve host 'gzip'
'
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.de/">here</A>.
</BODY></HTML>
~ user$ HEADERS="-H 'Accept:\ text/xml' -H 'Accept:Encoding:\ gzip'"
~ user$ curl $HEADERS google.de
curl: (6) Couldn't resolve host 'text'
curl: (6) Couldn't resolve host 'gzip''

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.de/">here</A>.
</BODY></HTML>
~ user$ HEADERS="-H "Accept:\ text/xml" -H "Accept:Encoding:\ gzip""
~ user$ curl $HEADERS google.de
curl: (6) Couldn't resolve host 'text'
curl: (6) Couldn't resolve host 'gzip'

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.de/">here</A>.
</BODY></HTML>
~ user$ HEADERS="-H \"Accept: text/xml\" -H \"Accept:Encoding: gzip\""
~ user$ curl $HEADERS google.de
curl: (6) Couldn't resolve host 'text'
curl: (6) Couldn't resolve host 'gzip"'

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.de/">here</A>.
</BODY></HTML>

What can I do to make it work ?
Thanks !

CRC123 08-21-2008 09:54 AM

No spaces between colons:
Code:

~> HEADERS="-H \"Accept:text/xml\" -H \"Accept:Encoding:gzip\""
~> echo $HEADERS
-H "Accept:text/xml" -H "Accept:Encoding:gzip"
~> curl $HEADERS google.de
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.de/">here</A>.
</BODY></HTML>

I don't know if thats what your after, but this is what I got and there are no errors

redvivi 08-21-2008 09:57 AM

Quote:

Originally Posted by CRC123 (Post 3254951)
No spaces between colons:
Code:

~> HEADERS="-H \"Accept:text/xml\" -H \"Accept:Encoding:gzip\""
~> echo $HEADERS
-H "Accept:text/xml" -H "Accept:Encoding:gzip"
~> curl $HEADERS google.de
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.de/">here</A>.
</BODY></HTML>

I don't know if thats what your after, but this is what I got and there are no errors

Thanks ! But if unfortunately I need these spaces ? Is there another solution ?

CRC123 08-21-2008 02:00 PM

Are you sure you need the spaces? That would be strange; for me at least.

redvivi 08-21-2008 02:02 PM

Quote:

Originally Posted by CRC123 (Post 3255204)
Are you sure you need the spaces? That would be strange; for me at least.

As long as they are sent ith spaces by the browser, I really need them, I really don't know what to do to solve this problem.


All times are GMT -5. The time now is 12:04 AM.