How to exec a complicated command?

  • Follow


$ wget --no-check-certificate --secure-protocol=SSLv3 --post-
data="userid=USERID&passws=PASSWD" -O /tmp/junk.log https://123.456.789.123
> /tmp/wget.log 2>&1

The wget command above works from a shell prompt, but when I tried the
following in tcl, it failed.

% set cmd "wget --no-check-certificate --secure-protocol=SSLv3 --post-
data=\"userid=USERID&passws=PASSWD\" -O /tmp/junk.log https://123.456.789.123
> /tmp/wget.log 2>&1"

% eval exec $cmd  #### This fails!

Can anyone please help? Thanks.

/Why Tea
0
Reply Why 4/25/2010 9:27:08 AM

At Sun, 25 Apr 2010 02:27:08 -0700 (PDT) Why Tea <ytlim1@gmail.com> wrote:

> 
> $ wget --no-check-certificate --secure-protocol=SSLv3 --post-
> data="userid=USERID&passws=PASSWD" -O /tmp/junk.log https://123.456.789.123
> > /tmp/wget.log 2>&1
> 
> The wget command above works from a shell prompt, but when I tried the
> following in tcl, it failed.
> 
> % set cmd "wget --no-check-certificate --secure-protocol=SSLv3 --post-
> data=\"userid=USERID&passws=PASSWD\" -O /tmp/junk.log https://123.456.789.123
> > /tmp/wget.log 2>&1"
> 
> % eval exec $cmd  #### This fails!
> 
> Can anyone please help? Thanks.

exec takes a *list* not a *string*:

set cmd [list wget --no-check-certificate --secure-protocol=SSLv3 \
	--post-data=\"userid=USERID&passws=PASSWD\" -O /tmp/junk.log \
	https://123.456.789.123 >&/tmp/wget.log]
eval exec $cmd

> 
> /Why Tea
>                                                                                              

-- 
Robert Heller             -- Get the Deepwoods Software FireFox Toolbar!
Deepwoods Software        -- Linux Installation and Administration
http://www.deepsoft.com/  -- Web Hosting, with CGI and Database
heller@deepsoft.com       -- Contract Programming: C/C++, Tcl/Tk

                                                                                                           
0
Reply Robert 4/25/2010 12:28:39 PM


On 25/04/2010 13:28, Robert Heller wrote:
> exec takes a *list* not a *string*:
>
> set cmd [list wget --no-check-certificate --secure-protocol=SSLv3 \
> 	--post-data=\"userid=USERID&passws=PASSWD\" -O /tmp/junk.log \
> 	https://123.456.789.123 >&/tmp/wget.log]
> eval exec $cmd

And if it gets too complicated, you can always use the shell to help:

   set cmd ...whatever...
   exec /bin/sh -c $cmd

I've found this to be particularly useful when executing shell commands
supplied by users.

Donal.
0
Reply Donal 4/25/2010 7:07:22 PM

Why Tea wrote:
> $ wget --no-check-certificate --secure-protocol=SSLv3 --post-
> data="userid=USERID&passws=PASSWD" -O /tmp/junk.log
> https://123.456.789.123
>> /tmp/wget.log 2>&1
> 
> The wget command above works from a shell prompt, but when I tried
> the following in tcl, it failed.
> 
> % set cmd "wget --no-check-certificate --secure-protocol=SSLv3
> --post- data=\"userid=USERID&passws=PASSWD\" -O /tmp/junk.log
> https://123.456.789.123
>> /tmp/wget.log 2>&1"
> 
> % eval exec $cmd  #### This fails!
> 
When translating a shell command line to a Tcl exec command you have 
to consider the quoting rules of both. The quotes in the original 
command are there to protect the & from the shell. Those quotes are 
not actually passed to the command. In Tcl the & (in that location) 
doesn't have any special meaning and doesn't have to be protected.

The easiest way to get the Tcl quoting of a command correct is to 
use the list command.

Also, the 2>&1 sequence has special meaning to the shell. You need 
to use the Tcl equivalent in your exec command. Robert Heller made 
that change in his reply, but he didn't put a space between the >& 
and the filename, which I think is needed.

Putting all this together, the following should work:
set cmd [list wget --no-check-certificate --secure-protocol=SSLv3 \
    --post-data=userid=USERID&passws=PASSWD -O /tmp/junk.log \
    https://123.456.789.123 >& /tmp/wget.log]
eval [linsert $cmd 0 exec]


Schelte.

0
Reply Schelte 4/25/2010 8:02:43 PM

* Robert Heller <heller@deepsoft.com>
| set cmd [list wget --no-check-certificate --secure-protocol=SSLv3 \
| 	--post-data=\"userid=USERID&passws=PASSWD\"

I don't think you need the " around the post-data here.  Usually they
are used on the SHELL to quote spaces or other shell=special chars in
the arguments.
Simply
  --post-data=userid=USERID&passws=PASSWD
should do the trick.

R'
0
Reply Ralf 4/26/2010 8:48:12 AM

On Apr 26, 6:48=A0pm, Ralf Fassel <ralf...@gmx.de> wrote:
> * Robert Heller <hel...@deepsoft.com>
> | set cmd [list wget --no-check-certificate --secure-protocol=3DSSLv3 \
> | =A0 =A0 =A0 --post-data=3D\"userid=3DUSERID&passws=3DPASSWD\"
>
> I don't think you need the " around the post-data here. =A0Usually they
> are used on the SHELL to quote spaces or other shell=3Dspecial chars in
> the arguments.
> Simply
> =A0 --post-data=3Duserid=3DUSERID&passws=3DPASSWD
> should do the trick.
>
> R'

Robert, you are absolutely right. Those quotes are the
problem in my case. Thanks!

Thanks also goes to the others who took the time to
explain the finer details and alternative solutions.

/Why Tea
0
Reply Why 4/26/2010 9:05:06 AM

5 Replies
705 Views

(page loaded in 0.002 seconds)

Similiar Articles:













7/26/2012 9:42:01 AM


Reply: