maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Off Topic (https://talk.maemo.org/forumdisplay.php?f=19)
-   -   Need advanced shell scripting help (https://talk.maemo.org/showthread.php?t=66465)

GameboyRMH 2010-12-02 16:01

Need advanced shell scripting help
 
Hey all, I'm about to finish a really awesome shell script (not Maemo-related, but still very handy) but one last thing is holding it up. I need to run wget '$url' where the content of the $url variable is actually substituted, but the single quotes prevent that. So the command should be run like:

Code:

wget 'http://site.com/whatever.html'
It is absolutely vital that I use single quotes here, just like this, or the operation won't work (long story about wget, but trust me there's no other way)

So what escape sequence can I use to make the contents of my variable come out between single quotes? Give me a working answer and I can share my handy script! :D

anthonie 2010-12-02 16:11

Re: Need advanced shell scripting help
 
Eh... Just a man entry...

Quote:

There are three quoting mechanisms: the escape character, single quotes, and double quotes. A non-quoted backslash (\) is the escape character. It preserves the literal value of the next character that follows, with the exception of <newline>.

If a \<newline> pair appears, and the backslash is not itself quoted, the \<newline> is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).

Enclosing characters in single quotes preserves the literal value of
each character within the quotes. A single quote may not occur Between single quotes, even when preceded by a backslash.

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, ‘, and \. The characters $ and ‘ retain their special meaning within double quotes. The backslash retains its special meaning only when followed
by one of the following characters: $, ‘, ", \, or <newline>. A double quote may be quoted within double quotes by preceding it with a backslash. When command history is being used, the double quote may not be used to quote the history expansion character.

Laughingstok 2010-12-02 16:22

Re: Need advanced shell scripting help
 
result=`wget \'${url}\'`
echo ${result}

:confused:

GameboyRMH 2010-12-02 17:09

Re: Need advanced shell scripting help
 
Quote:

Originally Posted by Laughingstok (Post 887671)
result=`wget \'${url}\'`
echo ${result}

:confused:

I'm trying to actually run the command though...

lma 2010-12-03 08:03

Re: Need advanced shell scripting help
 
Quote:

Originally Posted by GameboyRMH (Post 887648)
It is absolutely vital that I use single quotes here, just like this, or the operation won't work (long story about wget, but trust me there's no other way)

That sounds like an interesting story in itself, please elaborate :-)

I assume the URL in question contains "interesting" characters which cause some breakage when used with double quotes on the command line. Have you tried using wget -i instead?

GameboyRMH 2010-12-03 12:19

Re: Need advanced shell scripting help
 
Quote:

Originally Posted by lma (Post 888167)
That sounds like an interesting story in itself, please elaborate :-)

I assume the URL in question contains "interesting" characters which cause some breakage when used with double quotes on the command line. Have you tried using wget -i instead?

This is basically the problem I'm having:

http://www.planetmike.com/2005/04/12/wget-and-urls-with-ampersands/

I'll give wget -i a try, but putting the URLs in a file would be highly impractical...this script iterates through a list of URLs using cut.

kureyon 2010-12-03 13:05

Re: Need advanced shell scripting help
 
Quote:

Originally Posted by GameboyRMH (Post 888304)
This is basically the problem I'm having:
...

Huh? That page just displays "No"??

Anyway something like:

Code:

URL="http://www.example.com/some web page.php?a=1&b=2&c=3"
wget "${URL}"

works for me.

GameboyRMH 2010-12-03 13:22

Re: Need advanced shell scripting help
 
I fixed the URL and I'm now working on the script...

lma 2010-12-03 13:23

Re: Need advanced shell scripting help
 
Quote:

Originally Posted by GameboyRMH (Post 888304)
This is basically the problem I'm having:

http://www.planetmike.com/2005/04/12...th-ampersands/

Ampersands are neutered just fine in double quotes as well:

Code:

$ wget -nv "http://talk.maemo.org/attachment.php?attachmentid=16109&d=1291329691"
13:27:52 URL:http://talk.maemo.org/attachment.php?attachmentid=16109&d=1291329691 [12750/12750] -> "attachment.php?attachmentid=16109&d=1291329691" [1]

Quote:

I'll give wget -i a try, but putting the URLs in a file would be highly impractical...this script iterates through a list of URLs using cut.
-i can also be used to accept URLs fed to it via stdin, eg:

Code:

$ long-pipeline-using-cut | wget -i -

GameboyRMH 2010-12-03 13:34

Re: Need advanced shell scripting help
 
WOOHOO Success! I had one other problem but wget "${URL}" did the trick! :D

Stand by for sweet script! :cool:


All times are GMT. The time now is 14:27.

vBulletin® Version 3.8.8