Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Script to mount SMB share

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)

Posted on Nov 9, 2012 12:35 PM

Reply
Question marked as Best reply

Posted on Nov 9, 2012 12:47 PM

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.

5 replies
Question marked as Best reply

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.

Nov 9, 2012 1:08 PM in response to etresoft

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.

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.

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.

Nov 13, 2012 8:27 AM in response to etresoft

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.

Script to mount SMB share

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.