Skip navigation

Script to mount SMB share

1491 Views 5 Replies Latest reply: Nov 13, 2012 8:27 AM by AdamBritt RSS
AdamBritt Level 2 Level 2 (225 points)
Currently Being Moderated
Nov 9, 2012 12:35 PM

Hello all,

 

I am trying to write a script to mount an SMB share to a user's folder when they log in to one of the Mac clients. My script is working for the most part, but sometimes when it is run the folder on the desktop disappears. If I navigate to the Desktop folder in Finder, it is there.

 

I have been searching here and other websites on the internet but I haven't been able to find an answer to this problem. I am hoping someone might have an idea. The script is below.

 

#!/bin/bash

 

cur_user=`/bin/ls -l /dev/console | awk '/ / { print $3 }'`

 

if [ ! -d /Users/$cur_user/Desktop/Home ]

    then

        mkdir /Users/$cur_user/Desktop/Home

    else

        mount -t smbfs //10.210.0.13/staff/$cur_user /Users/$cur_user/Desktop/Home

fi

 

exit 0

MacBook Pro, OS X Mountain Lion (10.8)
  • etresoft Level 7 Level 7 (23,880 points)
    Currently Being Moderated
    Nov 9, 2012 12:47 PM (in response to AdamBritt)

    I recommend using AutoFS for this kind of thing: http://images.apple.com/business/docs/Autofs.pdf

     

    According to the script, it will only mount the directory if the desktop Home directory already exists. If the desktop Home directory doesn't exist, it will create it, but nothing more. You need to use "mkdir -p" to always attempt to create the directory. The "-p" flag will allow the program to be successful even if the directory already exists.

     

    You should use $USER instead of that funky awk script. Same goes for $HOME for the home directory. Don't rely on bash either. Really, all you need is:

     

    #/bin/sh

     

    mkdir -p $HOME/Desktop/Home

    mount -t smbfs //10.210.0.13/staff/$USER $HOME/Desktop/Home

     

    But again, AutoFS would be far more elegant.

  • etresoft Level 7 Level 7 (23,880 points)
    Currently Being Moderated
    Nov 9, 2012 1:13 PM (in response to AdamBritt)

    There is another option too. You can mount the volume in the Finder with Go > Connect to Server and then create a alias (drag while holding down option and command) to the volume or anything inside it. Then, when you open the alias, it will automatically mount the network volume. You don't have control over where it mounts the volume in this case, however.

  • BobHarris Level 6 Level 6 (12,495 points)
    Currently Being Moderated
    Nov 9, 2012 2:34 PM (in response to etresoft)

    You don't have control over where it mounts the volume in this case, however.

    Yea, but the volume mounted /Volumes/TheName should show up on the User's Desktop anyway, unless the Finder has been told to not put the volumes on the Desktop.

     

    And a symlink could always put in the Desktop folder that points  to /Volumes/TheName.

     

    AutoFS would most likely still be better.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.