5 Replies Latest reply: Nov 13, 2012 8:27 AM by AdamBritt
AdamBritt Level 2 Level 2 (225 points)

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 (27,125 points)

    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.

  • AdamBritt Level 2 Level 2 (225 points)

    Thanks for the suggestion. I did read over the AutoFS whitepaper in my quest, but I was looking for something specific at the time so I think I will read it over again. I'm new to anything UNIX so this was all hobbled together with pieces I found elsewhere. I will give it a shot Monday morning and let you know how it goes.

  • etresoft Level 7 Level 7 (27,125 points)

    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 (15,965 points)

    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.

  • AdamBritt Level 2 Level 2 (225 points)

    I got it working now for the most part. I took the if then statement out of my script and added a line to change the Show Connected Servers option in Finder to on, which solved the problem of the folders disappearing. A few times it acted weird, where a folder would show up twice on the desktop but only one would open. The second icon would do nothing and I couldn't remove it, but it will be something I'll work out later.

     

    Thanks for the help guys.