samba-mounted directory, OS X, bash, 'system' calls

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Have got a Perl script called from within a bash script.
Bash connects to a Samba share:

umount ~/Documents/bin/pdfs
mount -t smbfs //NAME:pass@server/SHARE ~/Documents/bin/pdfs

Then it calls this Perl script:

#!/usr/bin/perl -w
my $sourcedir="~/Documents/bin/pdfs";
print "sourcedir=$sourcedir\n";

I get:

ls: ~/Documents/bin/pdfs: No such file or directory

However if I copy the path from the error message and paste it onto
the command line behind 'ls' [  ls ~/Documents/bin/pdfs ], then the
shell lists the directory.

In an earlier try, I mounted the Samba volume via the Mac GUI and
pointed the script to something like /Volumes/pdfs. In that case it
found the files I was looking for.

Since I want this to run on a crontab and can't count on the volume
being mounted in the middle of a random night, I wanted to script the
Samba connection. Since I couldn't count on the volume NOT being
mounted either, I begin by unmounting anything that might already be
at that point.

John Campbell

Re: samba-mounted directory, OS X, bash, 'system' calls

Quoted text here. Click to load it

Your shell is expanding ~/Documents/bin/pdfs into something like
/Users/jfcampbell/Documents/bin/pdfs. Passing a list to system bypasses
the shell (normally a good thing) so ls ends up looking for a directory
called '~' under the current working directory.

You need to expand the '~'. The simplest way is probably

    $sourcedir =~ s/^~/$ENV/;

though that won't respect the ~user/foo syntax. Another way would be

    $sourcedir = glob $sourcedir;

which will expand ~user but will also expand other wildcards like *. I
don't know of any module that will just expand ~user and leave wildcards
alone, but it would be a pretty simple function to write using getpwnam.


Re: samba-mounted directory, OS X, bash, 'system' calls

Quoted text here. Click to load it

Or, I can spell out /Users/jfcampbell/Documents/bin/pdfs as a

Though I can't count on the Samba share being mounted all the time, I
can be reasonably sure the user name will be good for the life of the


Site Timeline