Bash scripts examples : Mount server’s share + informative pop-up

Introduction

Here is a new chapter of the “Bash script examples” series :

This is a script that i use on my personal computer (at home) to mount my server’s share (in order to access my music and downloads)

 

More “bash scripts examples” posts

 

 

1 Description

This little script pops up a little message in the notification area to keep you informed about how the process ends up : if the mount operation was complete or if any problems occurs : such as server not exporting the directory you were trying  to mount or the mount point did not exist etc.

You can adapt this script by editing the first two variables (arrays at lines 27 & 31) which contains the exported directories and/or the mount points.

The server address or IP address is contained in the $server variable.

I use this script as a target for a desktop icon, you can use in any ways you wish…

Feel free to propose any evolution or tell us why you think this script is good or bad !

 

2 The code

 

#!/bin/bash

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Company name - Division name - Project name
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# HallOfTips - 18/01/2013 - ServerMount.bash - v1.0
# ---------------------------------
# This script mounts the ${server} shares to the specified mount_point 
#+ and gives information using a pop-up in the notification area.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#####################################
##### TODO
#####################################

#####################################
##### EXIT CODES
#####################################
# 1 = unspecified error
# 2 = arrays have a different numbers of elements

#####################################
##### VARIABLES
#####################################
server="server"							                    # Server's name
exported_dirs=( "/media/Safe/Sounds" "/media/Downloads" )	# Exported dirs on ${server}, given as an array
								                            #+ Please note that it is important to specify items in the same order
								                            #+ as in the ${mount_points[*]} array

mount_points=( "/media/Sounds" "/media/Downloads" )		    # mount point on local machine, given as an array
								                            #+ Please note that it is important to specify items in the same order
								                            #+ as in the ${exported_points[*]} array

##### MESSAGES
Array_diff="Arrays DOES NOT have the same number of elements, exiting..."
Mount_OK="is now mounted"
Mount_Nok="could not be mounted"
Exported_Nok="is not exported on"
MountPoint_Nok="Mount point does not exist"
Already_Mounted="is already mounted"

#####################################
##### FUNCTIONS
#####################################

MountPointChk() {
  # This function check if the given argument exists AND is a directory
  local mountpoint="${1}"
  if [[ ! -d ${mountpoint} ]] ; then
    kdialog --passivepopup "Error : ${MountPoint_Nok} (${mountpoint})" && return "1"
  fi
}

ExportChk() {
  # This function check if the given argument is exported by ${server}
  local exported="${1}"
  if ! showmount -e ${server} |grep "${exported}" &>/dev/null ; then
    kdialog --passivepopup "Error : ${exported} ${Exported_Nok} ${server}" && return "1"
  fi
}

mountedChk() {
  # This function checks if the given argument is already mounted
  local mounted="${1}"
  if mount |grep ${mounted} &>/dev/null ; then
    kdialog --passivepopup "Info : ${mounted} ${Already_Mounted}" && return "1"
  fi
}

MountDir() {
  # This function mount each given arguments (must be given as $1=exported_dir $2=mount_point)
  local exported_dir="${1}"
  local mount_point="${2}"
  if sudo mount ${server}:${exported_dir} ${mount_point} &>/dev/null ; then
    kdialog --passivepopup "${exported_dir} ${Mount_OK} on ${mount_point}"
  else
    kdialog --passivepopup "Error : ${exported_dir} ${Mount_Nok} on ${mount_point}"
  fi
}

#####################################
##### MAIN
#####################################

# First check that the mount_points AND the exported_dirs have the same nulber of elements (for each mount_points there must be an exported_dirs)
if [[ "${#mount_points[@]}" -ne "${#exported_dirs[@]}" ]] ; then
  echo "${Array_diff} (\"${#mount_points[@]}\" \"${#exported_dirs[@]}\")" && exit 2
fi

# Start iterating on mount_points array
for (( i="0"; i <= ${#mount_points[@]}; i++ )) ; do	          # Iteration will start from 0 up to 
							                                   #+ the total number of elements in mount_points array

  # First check if required dirs are exported by ${server} 
  #+ (required dirs are specified within the exported_dirs array)
  for ExportDir in ${exported_dirs[${i}]} ; do
    if ! ExportChk ${ExportDir} ; then 			               # if it is NOT correctly exported 
      continue 2					                           #+ we should then jump to the next element in the top-level loop
    fi

    # Then check if the required mount point exists
    for MPoint in ${mount_points[${i}]} ; do
      if ! MountPointChk ${MPoint} ; then                      # if mount point does not exist 
	    continue 3                                             #+ we should then jump to the next element in the top-level loop
      fi

       # We are now done for exported dirs check, still the  mount_points dirs check
       for MountPoint in ${mount_points[${i}]} ; do
         if ! mountedChk ${MountPoint} ; then		           # if it is already mounted
	       continue 4					                       #+ we should then jump to the next element in the top-level loop
         else
	       MountDir ${ExportDir} ${MountPoint}	               # If we reach this point, we just have to mount it
         fi

        done
     done
  done
done

Leave a Reply

Your email address will not be published. Required fields are marked *

This site supports SyntaxHighlighter via WP SyntaxHighlighter. It can highlight your code.
How to highlight your code: Paste your code in the comment form, select it and then click the language link button below. This will wrap your code in a <pre> tag and format it when submitted.