-   Other *NIX (
-   -   Bash variable problem: cURL -b parameter (string form e.g. "name=value;n2=v2") (

sithemac 07-08-2008 04:24 PM

Bash variable problem: cURL -b parameter (string form e.g. "name=value;n2=v2")
Hi, sorry this is a newbie questions I fear:

I am trying to update one of my bash scripts to work with Firefox3's sqlite cookie format.

I can extract the cookie values but am trying to pass them as a string variable to the -b parameter in curl, rather than as a reference to a netscape format cookie.txt file.

The curl MAN page explains that this is possible and it works if I pass the hardcoded string cURL -b"pass=xyz;userID=123", but when I pass the same data as a variable it breaks. I've tried all manner of escaping, quoting, echoing the variable and using brackets and backticks but am really stuck.

I'm building the string by querying sqlite for the individual values:

  ffcf=[path to cookies.sqlite]
  thepass=`sqlite3 "$ffcf" "select value from moz_cookies where host='[host string]' and name='pass'"`
  theUID=`sqlite3 "$ffcf" "select value from moz_cookies where host='[host string]' and name='uid'"`

then concatenating them into a variable

  echo "Input Cookie String:" "$inputcookiefn"

which looks right when I echo it back

so my curl command looks like:


curl -s -S -b "$inputcookies"  -c newcookies.txt -A 'Mozilla/4.0' [website URL here] > testresults.htm
The cURL command completes, but the site doesn't accept the cookies when passed as a variable, although it accepts the hardcoded string as shown above, which _appears_ to be identical.

I am trying to avoid the need to generate a cookies.txt file from the sqlite queries and just build a string value for the -b parameter.

Any help or pointers much appreciated.

Mr. C. 07-08-2008 06:12 PM

This problem comes up frequently, as new users are not familiar with how and when the shell splits words. You have also discovered, and may not yet realize it, that echo is not a good too to show what the shell sees. Use set -x instead before your assignment, and learn what its output means and indicates.

This will also help:

See Question 8, Homework, Week 4 "Arguments":

sithemac 07-09-2008 05:56 PM

Thanks, Mr C :)

The URL is a great one for me. I haven't had time to work through the course yet, but it looks very useful. Suffice to say, I don't know enough of the building blocks to understand all the code in lesson 4, as I've skimmed a couple of UNIX 101 type courses but bounced off power-globbing, iteration and such topics without really understanding them. Amazing (to me) that I've actually written some functional, useful scripts with such limited understanding and have so far avoided any major catastrophes ;)

After my previous post I'd done a bit more digging and discovered the set -x debug tool, which was enough for me to see that as I'd suspected, echo was not accurately representing the variable. However, just as you suggest, I now need to learn the meaning of its output, which will require some further experimentation.

Unfortunately, I only get time to do this stuff in the last couple of hours of consciousness each day after coming home from working on VB and SQL, so my brain is not at its best.

Thanks again for providing such a useful reference. I'll post on this thread again when I've solved my little problem.

Mr. C. 07-09-2008 06:15 PM

You're welcome. Glad you find the material useful. Every so often, I consider updating the material to make the lessons more accessible, and geared towards bash.

All times are GMT -5. The time now is 03:23 AM.