Script to Mount Windows Shares at Logon

Write tutorials here
There are more tutorials here
Forum rules
Please don't add support questions to tutorials,start your own thread in the appropriate sub-forum instead. Before you post please read this

Script to Mount Windows Shares at Logon

Postby Fergsauce on Wed Jun 20, 2012 10:42 am

I have found the old method of editing fstab to auto-mount shares at startup not useful for a NAS that goes to sleep after a period of time. The NAS takes a few seconds to startup and the fstab mount fails.

So I tried a different method, and made a little script to mount them at logon. Then I added more to it.. then some more.. and I thought maybe someone out there would benefit from it. It also has room for improvement probably.

If you need permissions to the Windows share, connect to it via nautilus by going to 'File' > 'Connect to Server...' and fill out the information. Make sure you check the 'Remember Password' checkbox once you enter the correct credentials. Also, unmount it if you plan on testing this script.

1. Paste the following code in a file somewhere in your home directory and name it (or whatever you want)
2. Make it executable (chmod +x <path_to_file>)
3. Open 'Startup Applications' and in the command field enter the path to the script file followed by the parameters it requires.

(for parameters just run it in the command line: bash <path_to_file>)

Code: Select all
# by Fergsauce
# This (bash) shell script is used to mount Windows shares and create symlinks
# to them in your home directory for easy access.
# If permissions are required, first open the share through nautilus by
# clicking "File" > "Connect to Server..." and connecting to the desired
# server. Make sure to save the username and password used.
# To use this at logon, open 'Startup Applications', click 'Add' and in the
# command field, enter the path to this file followed by the parameters you want.
# Run with no parameters to see usage.


   echo -ne $reset_color
   if [ "$1" != "0" ]; then
      echo -e "Error: $1\n"
   exit $1

   echo -e $reset_color"\nUsage:\nbash [-s -S] <ip_of_server> [-n -N] <share_name>"
   echo -e "Optional parameter: [-f -F] <path_to_symlink> (The default folder is ~/Shares/<share_name>)\n"
   echo "Examples:"
   echo "sh -s -n Shared"
   echo -e "sh -s -n Documents -f /Shares/NAS/Documents\n"
   fnExit 1

if [ $# == 0 ]; then

if [ "$1" == "-s" ] || [ "$1" == "-S" ]; then
elif [ "$3" == "-s" ] || [ "$3" == "-S" ]; then
elif [ "$5" == "-s" ] || [ "$5" == "-S" ]; then

if [ "$1" == "-n" ] || [ "$1" == "-N" ]; then
elif [ "$3" == "-n" ] || [ "$3" == "-N" ]; then
elif [ "$5" == "-n" ] || [ "$5" == "-N" ]; then

if [ "$1" == "-f" ] || [ "$1" == "-F" ]; then
elif [ "$3" == "-f" ] || [ "$3" == "-F" ]; then
elif [ "$5" == "-f" ] || [ "$5" == "-F" ]; then

if [ -z $server_ip ]; then
   echo -e $color_red"Could not read the server ip"$reset_color

if [ -z $share_name ]; then
   echo -e $color_red"Could not read the share name"

if [ ! -d ~/$folder ]; then
   echo "Making the local folder: $folder"

mkdir -p ~/$folder

if [ ! -d ~/$folder ]; then
   echo -e $color_red"Could not create target directory: ~/$folder"
   fnExit 2

echo "Mounting $share_name..."
gvfs-mount smb://$server_ip/$share_name

while [ $i -lt 10 ] && [ ! -d ~/".gvfs/$share_name on $server_ip/" ]
sleep 1

if [ ! -d ~/".gvfs/$share_name on $server_ip/" ]; then
   echo -e $color_red"gvfs folder not found: "~/".gvfs/$share_name on $server_ip/"$reset_color
   echo -e "Check if there were errors mounting the share, or if it is located in the ~/.cache directory.\n"
   fnExit 3

if [ ! -e ~/"$folder/$share_name" ] && [ -L ~/"$folder/$share_name" ]; then
   echo "Creating symlink..."
   ln -s -f ~/".gvfs/$share_name on $server_ip/" ~/"$folder/$share_name"

if [ ! -e ~/"$folder/$share_name" ] && [ -L ~/"$folder/$share_name" ]; then
   echo "Attempting to fix gvfs..."
   fusermount -zu ~/.gvfs
   /usr/lib/gvfs/gvfs-fuse-daemon ~/.gvfs

if [ ! -e ~/"$folder/$share_name" ] && [ -L ~/"$folder/$share_name" ]; then
   echo -e $color_red"Error creating symlink. Try manually."
   fnExit 4

echo -e $color_green"Success! Your share is mounted and can be access at:"$reset_color
echo -e ~/"$folder/$share_name\n"
fnExit 0

*Edit: It should be noted that I am not a shell programmer by trade.

Also, goes without saying, but... This script is provided "as-is" and makes no warranty of any kind. Use of the information in this post is at your own risk.
User avatar
Level 1
Level 1
Posts: 28
Joined: Thu Jan 28, 2010 9:55 pm

Linux Mint is funded by ads and donations.

Return to Tutorials

Who is online

Users browsing this forum: Google Feedfetcher and 11 guests