Tim Schmidt
Tim Schmidt

Reputation: 2015

Parameters getting combined in an odd way

I'm trying to mount an smb share from inside a bash function. The following works from a prompt:

mount_smbfs "//domain;[email protected]/share" ~/mnt/share

Inside my function, I have it scripted as:

mount_smbfs "//domain\;user@$2" $HOME/mnt/$1

I haven't gotten it to work. It just outputs the usage for the mount command. So I added a set -x to the start of my function and I can see the command getting executed as:

'mount_smbfs //domain\;[email protected]/share' /Users/user/mnt/share

So it looks like the second parameter to mount_smbfs isn't getting passed. Can anyone tell me why not?

UPDATE

Here's my function:

mnt-prod() {
    set -x
    umount $HOME/mnt/$1
    mkdir -p $HOME/mnt/$1
    mount_smbfs "//domain\;user@$2" $HOME/mnt/$1
    cd $HOME/mnt/$1
}

Here's the alias I'm trying to use:

alias mnt-share="mnt-prod share foo.domain.com/share;"

SOLUTION

I ended up with a slightly modified version of the great answer by @ sjnarv:

mnt-prod() {
    local dir="$1" host="$2" share="$3"

    umount "$HOME/mnt/$dir" || true
    mkdir -p "$HOME/mnt/$dir" && \
        mount_smbfs "//production;tas@$host/$share" "$HOME/mnt/$dir" && \
        cd "$HOME/mnt/$dir"
}

Upvotes: 0

Views: 248

Answers (1)

sjnarv
sjnarv

Reputation: 2374

The first comment above from @chepner still looks the most relevant. If I leave the \ character present before the ; I see an error like:

mount_smbfs: URL parsing failed, please correct the URL and try again: Invalid argument

Longer Answer

The use of mount_smbfs seems to imply the use of MacOS (versus something like mount.cifs on various Linux distributions). My comments here reflect trying things out on a Mac.

From the usage message / man page for mount_smbfs:

mount_smbfs [-N] [-o options] [-d mode] [-f mode] [-h] [-s] [-v]
            //[domain;][user[:password]@]server[/share] path

Your function provides values for domain, user, server, and share, and constructs path using a local convention around the share value.

My network environment has its own location-specific details, so rather than hard-wire my localisms into mnt-prod, I instead updated the function to use a few more arguments to construct the eventual mount_smbfs command in a more generic way. IMO this makes it more flexible, and easier to read and maintain.

Consider the following, which:

  • uses local variables to track/document function args 1 through 4
  • more consistently quotes values (perhaps you'll eventually have a share name with a space in it)
  • uses the || true idiom to let the umount command fail benignly in the presence of the errexit option (set -e)
  • uses && to create a mkdir && mount_smbfs && cd command list that aborts at the first command failure

The suggested updates:

mnt-prod() {
    local domain="$1" user="$2" host="$3" share="$4"

    set -x

    umount "$HOME/mnt/$share" || true

    mkdir -p "$HOME/mnt/$share" && \
    mount_smbfs "//$domain;$user@$host/$share" "$HOME/mnt/$share" && \
    cd "$HOME/mnt/$share"

    set +x
}

If my local Mac user name is jdoe, my domain name is MYDOMAIN, my Samba/SMB/CIFS server is my.domain.net, my username guest, and share name public, usage looks like:

$ mnt-prod MYDOMAIN guest my.domain.net public
+ umount /Users/jdoe/mnt/public
umount: /Users/jdoe/mnt/public: not currently mounted
+ true
+ mkdir -p /Users/jdoe/mnt/public
+ mount_smbfs '//MYDOMAIN;[email protected]/public' /Users/jdoe/mnt/public
+ cd /Users/jdoe/mnt/public
+ set +x
$

...and the mount succeeds: local path /Users/jdoe/mnt/public provides access to the remote share public.

Upvotes: 1

Related Questions