Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have written the following script which you may find useful if running Ubuntu on VirtualBox and make use of NetBeans. The scripts may even be used for other VirtualBox needs as it tries to:

  1. Install an application (NetBeans).
  2. Tests if Ubuntu is running with guest additions.
  3. Tries to mount a shared folder based on the default project name which typically remains the same on any system (host or guest).
  4. Writes the mount to rc.local for use friendliness.
  5. Creates and/or appends to a file in the users bin to allow the folder to be unmounted.

The script works flawlessly unless you upgrade. In such a scenario, multiple mounts will be done and the unmount script will not function as intended as there will be multiple entries in /etc/mtab.

This cannot be solved with use of umount flags like -f -l -a -t as typically this may result in all mounts being unmounted. umount -a -t vboxsf on the /target Is not the desired solution either as it may not be necessary to unmount all shared folders.

I have come up with a solution, but am new to bash/scripting and don't know how to implement it. My solution is to test rc.local for potential duplicate lines to avoid duplicate mounting:

#Author: Yucca Nel
#Will restart system
export PATH

#Modify these variables as needed...

echo "Provide NetBeans version (7.0.1 is default) then hit [Enter] :"
  read NetBeansVersion

  if [ -z "$NetBeansVersion" ]

mkdir -p /$tempWork;
cd /$tempWork;

sh $tempWork/*sh;

#Add Netbeans launcher to your PATH. Doing so allows you to run 'netbeans' command from the terminal
#This line will need to be changed if you changed the default install location (IOW Netbeans is not in ~/)
sudo ln -f -s ~/netbeans-$NetBeansVersion/bin/netbeans /usr/bin/;

#If you use VirtualBox , you can share your projects between Host and guest. Name of shared
#folder must match 'NetBeansProjects'
mkdir -p $HOME/NetBeansProjects

if [ -f /sbin/mount.vboxsf ]
    sudo /sbin/umount /home/$HOME/NetBeansProjects
    sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects

if mountpoint -q ~/NetBeansProjects
#Add it to the universal start script to automate process...
    sudo sed -ie '$d' $startupScript
    echo "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects"| sudo tee -a $startupScript
    echo "exit 0"| sudo tee -a $startupScript
    sudo chmod +x $startupScript

#Create a mount and unmount script file and add it to users local bin
    rm -rf $tempWork/*
    echo '#!/bin/bash' > $tempWork/
    echo '#!/bin/bash' > $tempWork/
    echo '#!/bin/bash' > $tempWork/
    echo '#!/bin/bash' > $tempWork/
    echo "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects" >> $tempWork/
    echo "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects" >> $tempWork/
    echo "sudo umount $HOME/NetBeansProjects" >> $tempWork/
    echo "sudo umount $HOME/NetBeansProjects" >> $tempWork/
    echo "exit 0" >> $tempWork/
    echo "exit 0" >> $tempWork/
    echo "exit 0" >> $tempWork/
    echo "exit 0" >> $tempWork/

    sudo chmod +x $tempWork/*
    sudo mv -f $tempWork/*.sh /usr/local/bin
    rm -rf $tempWork

#This function is used to cleanly exit with an error code.
function error_exit {
    sleep 7
    exit 1
sudo reboot
exit 0

Any pointers? My goal is to write a super scripts for Java developers that downloads most needed tools onto any Linux (not just Ubuntu) and mount potential things that don't need to be reinstalled if you have an existing development host. Things like Maven, Tomcat, SVN, JBoss don't need special guest install if they are already on host system and there are further benefits in combining different systems into one; e.g. Windows can run Photoshop and Safari browser, but Linux provides better customization and tools like ssh out the box.

share|improve this question

I am not sure if I understand... but here I count the number of unique lines from grep output.

grep "sudo /sbin/mount.vboxsf" /etc/rc.local | sort | uniq -c | wc -l

Two come from the echo lines, and two from the command lines. It should always equal four, yes?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.