Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi! I need to mount a window share on a network. im using fedora 13. i created a perl script myscript.pl. this is its content:
my $cmd = "sudo mount -o username='admin',password='',rw,uid='customer +',gid='customer' '//192.168.1.103/shared' '/home/customer/mnt'"; my $res = `$cmd`;
if i ran the script on a terminal (eg. open terminal ang type perl myscript.pl) it was able to mount properly. but the problem here is that if i called the script via a shortcut (launcher) it simply doesnt mount. not sure what is happening. so i tried this..
my $cmd = "sudo mount -o username='admin',password='',rw,uid='customer +',gid='customer' '//192.168.1.103/shared' '/home/customer/mnt'"; my $res = `gnome-terminal -e "$cmd"`;
it now works, but the problem is that i dont know how to wait for the mounting. my script simply opens a gnome-terminal then continues with the script and exits. it didnt even wait for the mounting to complete. i need to catch if it mounted or not. so how can i do this?

Replies are listed 'Best First'.
Re: mount cifs using perl and sudo and mount
by JavaFan (Canon) on Aug 28, 2010 at 11:06 UTC
    i need to catch if it mounted or not. so how can i do this?
    You'd use the exit code of the mount command. sudo's exit code will be of whatever command you issued, so checking the exit code of sudo should tell you whether the mount succeeded. If it isn't 0, check the manual page of mount to see what the exit code means.
Re: mount cifs using perl and sudo and mount
by dasgar (Priest) on Aug 28, 2010 at 05:19 UTC

    I'm not very familiar with Linux environments, but here's my guess as to what's happening.

    The back ticks are similar to system in that it runs the command and waits for the command to return control before allowing the script to continue. I believe what is happening is that the gnome-terminal is launching a new terminal console and is providing it $cmd's value and then is immediately returning control, which is why your script is not waiting after issuing the command.

    I'd recommend one of the following.

    • Modify the last line to be: my $res = `$cmd`; -- This skips opening in a new console and your script will wait for mount to return control before continuing.
    • From Perl, issue the mount command with no options inside back ticks. This will provide you with a list of mounted mount points. If /home/customer/mnt is not listed, then you don't have it mounted.
    • Use something like IPC::Open3 to be able to run the mount command and to be able to read in from STDOUT and STDERR.

Re: mount cifs using perl and sudo and mount
by oko1 (Deacon) on Aug 28, 2010 at 14:46 UTC

    You can't get the status of the command by using backticks; that's not what they're for (they're used if you want to capture the output of the command.) If you want the status, you need to use the 'system' function. Here, I'm going to loop-mount an ISO file on a directory in /tmp, then unmount it - with error checking:

    #!/usr/bin/perl -w use strict; system "sudo mount $ARGV[0] /tmp/mnt -o loop=/dev/loop0" and die "mount: ", $? >> 8, "\n"; print "Mounted successfully.\n"; system "sudo umount /tmp/mnt -l" and die "umount: ", $? >> 8, "\n"; print "Unmounted successfully.\n";

    And here's the output:

    ben@Jotunheim:/tmp$ ./mt ubuntu_9_10.iso Mounted successfully. Unmounted successfully. ben@Jotunheim:/tmp$ ./mt fake_filename fake_filename: No such file or directory mount: 32

    The return code from 'mount', 32, is listed in the man page as 'mount failure'.


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
      hi! thanks for the reply, but the problem if I didnt use the gnome-terminal is that i could never run my program inside a launcher. i could use the "run in terminal" mode. but i cannot use that because the program that i am using it (a very big program) cannot be run using "run in terminal" mode. so for my mount to work, i should be able to launch it without the terminal. your codes works fine if typed in a terminal. but it doesnt mount if there are no terminal. i thinks the sudo has something to do with it. but im not really sure.