Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   system(@args) query, result not as expected. (http://www.velocityreviews.com/forums/t909866-system-args-query-result-not-as-expected.html)

Justin C 03-04-2009 11:16 AM

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

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.

Josef Moellers 03-04-2009 12:01 PM

Re: system(@args) query, result not as expected.
 
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

Tad J McClellan 03-04-2009 12:09 PM

Re: system(@args) query, result not as expected.
 
Justin C <justin.0903@purestblue.com> 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/"

Krishna Chaitanya 03-04-2009 12:15 PM

Re: system(@args) query, result not as expected.
 
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

Josef Moellers 03-04-2009 01:50 PM

Re: system(@args) query, result not as expected.
 
Chris Mattern wrote:
> On 2009-03-04, Justin C <justin.0903@purestblue.com> 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

Krishna Chaitanya 03-04-2009 03:30 PM

Re: system(@args) query, result not as expected.
 
>
> 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.

Justin C 03-04-2009 04:04 PM

Re: system(@args) query, result not as expected.
 
On 2009-03-04, Justin C <justin.0903@purestblue.com> 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.


All times are GMT. The time now is 08:32 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.