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

J

Justin C

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.
 
J

Josef Moellers

Justin said:
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
 
T

Tad J McClellan

Justin C said:
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',
);
 
K

Krishna Chaitanya

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
 
J

Josef Moellers

Chris said:
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
 
K

Krishna Chaitanya

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.
 
J

Justin C

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top