Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > system(@args) query, result not as expected.

Reply
Thread Tools

system(@args) query, result not as expected.

 
 
Justin C
Guest
Posts: n/a
 
      03-04-2009

I have the following:

my @args = ("mount", "-t smbfs -o username=boris,password=drowssap",
"\"//255.255.255.255/Some Share with spaces\"", "/mnt/foo");

The error is *nix, not perl, but, please bear with me and read on. Here
is the error:

mount: unknown filesystem type 'password=drowssap"'

I chaned @args to be one string only:

my @args = ("mount -t smbfs -o username=boris,password=drowssap \"//255.255.255.255/Some Share with spaces\" /mnt/foo");

And it all works just fine. I thought that maybe @args weren't being
presented in order so a line just prior to 'system' I did a print, all
looked OK.

Any ideas why the mount command was thinking that 'password' was the
filesystem type?

Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      03-04-2009
Justin C wrote:
> I have the following:
>
> my @args = ("mount", "-t smbfs -o username=boris,password=drowssap",
> "\"//255.255.255.255/Some Share with spaces\"", "/mnt/foo");
>
> The error is *nix, not perl, but, please bear with me and read on. Here
> is the error:
>
> mount: unknown filesystem type 'password=drowssap"'
>
> I chaned @args to be one string only:
>
> my @args = ("mount -t smbfs -o username=boris,password=drowssap \"//255.255.255.255/Some Share with spaces\" /mnt/foo");
>
> And it all works just fine. I thought that maybe @args weren't being
> presented in order so a line just prior to 'system' I did a print, all
> looked OK.
>
> Any ideas why the mount command was thinking that 'password' was the
> filesystem type?


Because it's in the same argument to "mount" as the "-t"-option:

"More than one type may be specified in a comma separated list."
(man mount).

I.e. "mount" thinks that you passed the following to specify the vfstype:

"-t smbfs -o username=boris,password=drowssap"

which it parses as two vfstypes:
" smbfs -o username=boris" and "password=drowssap".

I haven't looked into the source of "mount", but maybe "mount" tries the
first, which fails, tries the second, which also fails, and then issues
an error message for the second only.

Try putting each argument into a separate string:

my @args = ("mount", "-t", "smbfs", "-o",
"username=boris,password=drowssap",
"\"//255.255.255.255/Some Share with spaces\"", "/mnt/foo");

Josef
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      03-04-2009
Justin C <(E-Mail Removed)> wrote:
>
> I have the following:
>
> my @args = ("mount", "-t smbfs -o username=boris,password=drowssap",
> "\"//255.255.255.255/Some Share with spaces\"", "/mnt/foo");
>
> The error is *nix, not perl, but, please bear with me and read on. Here
> is the error:
>
> mount: unknown filesystem type 'password=drowssap"'
>
> I chaned @args to be one string only:
>
> my @args = ("mount -t smbfs -o username=boris,password=drowssap \"//255.255.255.255/Some Share with spaces\" /mnt/foo");
>
> And it all works just fine. I thought that maybe @args weren't being
> presented in order so a line just prior to 'system' I did a print, all
> looked OK.
>
> Any ideas why the mount command was thinking that 'password' was the
> filesystem type?



The 2nd sentence of the docs for the function you are using
explains it:

perldoc -f system

...
Note that argument processing varies depending on the
number of arguments...

You want mount's 1st arg to be "-t" and its 2nd arg to be "smbfs" etc,
but you have given this 1st arg instead:

-t smbfs -o username=boris,password=drowssap

Also, you want one of the args to be

//255.255.255.255/Some Share with spaces

but have given it

"//255.255.255.255/Some Share with spaces"

instead.

When you are using system() with many args, there is no shell involved.

When there is no shell, there is no need to protect spaces from the
shell's argument processing, so you don't need or want the double quotes.



You need to make each argument a separate element in the array:

my @args = (
'mount',
'-t',
'smbfs',
'-o',
'username=boris,password=drowssap',
'//255.255.255.255/Some Share with spaces',
'/mnt/foo',
);


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Krishna Chaitanya
Guest
Posts: n/a
 
      03-04-2009
It happens because Perl starts $args[0] with arguments $args[1], $args
[2], ... as if in single quotes which disables shell's mechanism to
evaluate and tokenize the string. Single quoting preserves everything,
including spaces, and this is what happens:

Listing: 1.pl
=============

#!/usr/bin/perl

use warnings;
use strict;

my @args = ("ls","-l /tmp");
eval {
system(@args);
};
if ($@) {
print "Error: $@\n";
}

Output:
=======

ls: invalid option --
Try `ls --help' for more information.

Now see how this is equivalent to the following at command prompt:

[bld@BLD-RHEL5-32 perl_progs]# 'ls' '-l /tmp'
ls: invalid option --
Try `ls --help' for more information.

And see this:

[bld@BLD-RHEL5-32 perl_progs]# 'ls' '-l' '/tmp'
total 8
drwxr-xr-x 2 root root 4096 Mar 4 07:05 chumma
-rw-r--r-- 1 root root 3 Feb 27 15:20 TEST

The last example above works because even though they are single-
quoted, there are no preserved spaces (which are basically the cause
of the problem here). So, like, Josef said, pass each argument on its
own and don't include any spaces in quotes. Or, like you figured out,
pass everything as a single string, which will be checked for shell
metacharacters and will be invoked as /bin/sh -c <command>.

-Chaitanya
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      03-04-2009
Chris Mattern wrote:
> On 2009-03-04, Justin C <(E-Mail Removed)> wrote:
>> I have the following:
>>
>> my @args = ("mount", "-t smbfs -o username=boris,password=drowssap",
>> "\"//255.255.255.255/Some Share with spaces\"", "/mnt/foo");
>>
>> The error is *nix, not perl, but, please bear with me and read on. Here
>> is the error:
>>
>> mount: unknown filesystem type 'password=drowssap"'
>>
>> I chaned @args to be one string only:
>>
>> my @args = ("mount -t smbfs -o username=boris,password=drowssap \"//255.255.255.255/Some Share with spaces\" /mnt/foo");
>>
>> And it all works just fine. I thought that maybe @args weren't being
>> presented in order so a line just prior to 'system' I did a print, all
>> looked OK.
>>
>> Any ideas why the mount command was thinking that 'password' was the
>> filesystem type?

>
> Because you conflated the -o argument with the -t argument by making them
> all one argument. Try this:
>
> my @args = ("mount", "-t smbfs", "-o username=boris,password=drowssap",
> "\"//255.255.255.255/Some Share with spaces\"", "/mnt/foo");


This is only half-way and won't work:

"mount: unknown filesystem type ' smbfs'" (note the blank before "smbfs").

Josef
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
 
Reply With Quote
 
Krishna Chaitanya
Guest
Posts: n/a
 
      03-04-2009
>
> This is only half-way and won't work:
>
> "mount: unknown filesystem type ' smbfs'" (note the blank before "smbfs").


That's because of what I had said above, I guess.....any space present
in the list items will be preserved verbatim at the program invocation
time.
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      03-04-2009
On 2009-03-04, Justin C <(E-Mail Removed)> wrote:
>
> I have the following:
>
> my @args = ("mount", "-t smbfs -o username=boris,password=drowssap",
> "\"//255.255.255.255/Some Share with spaces\"", "/mnt/foo");


[snip]

Thank you all, very clear and concise. I have a better understanding of
the command now and shall implement it correctly.... and have, and all
is good.

Thank all, again, for your regular help.

Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i = 10; result = ++i - --i; How result become ZERO Lakshmi Sreekanth C Programming 52 09-23-2010 07:41 AM
Re: i = 10; result = ++i - --i; How result become ZERO Mr. Buffoon C Programming 4 09-23-2010 03:01 AM
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
simulation result is correct but synthesis result is not correct J.Ram VHDL 7 12-03-2008 01:26 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM



Advertisments