LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 08-20-2008, 02:06 PM   #1
redvivi
LQ Newbie
 
Registered: Aug 2008
Posts: 24

Rep: Reputation: 15
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
 
Old 08-20-2008, 03:06 PM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 31
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
 
Old 08-20-2008, 03:17 PM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
Old 08-20-2008, 03:20 PM   #4
redvivi
LQ Newbie
 
Registered: Aug 2008
Posts: 24

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
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.

Last edited by redvivi; 08-21-2008 at 08:56 AM.
 
Old 08-21-2008, 08:55 AM   #5
redvivi
LQ Newbie
 
Registered: Aug 2008
Posts: 24

Original Poster
Rep: Reputation: 15
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 !

Last edited by redvivi; 08-21-2008 at 08:57 AM.
 
Old 08-21-2008, 09:54 AM   #6
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 31
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
 
Old 08-21-2008, 09:57 AM   #7
redvivi
LQ Newbie
 
Registered: Aug 2008
Posts: 24

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by CRC123 View Post
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 ?

Last edited by redvivi; 08-21-2008 at 10:25 AM.
 
Old 08-21-2008, 02:00 PM   #8
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 31
Are you sure you need the spaces? That would be strange; for me at least.
 
Old 08-21-2008, 02:02 PM   #9
redvivi
LQ Newbie
 
Registered: Aug 2008
Posts: 24

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by CRC123 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script Variable Problem jimmy512 Programming 5 05-11-2007 07:38 AM
Problem with shell script using a variable from a unix command. abefroman Programming 1 05-10-2006 02:14 PM
Bash shell script - variable magic needed (no idea how to approach this) arrenlex Programming 8 03-02-2006 05:41 PM
problem with sed n variable in shell script dipenchaudhary Programming 4 02-12-2006 10:54 PM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM


All times are GMT -5. The time now is 04:00 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration