Skip to content

Instantly share code, notes, and snippets.

@xupyprmv
Last active August 29, 2015 13:56
Show Gist options
  • Save xupyprmv/9322394 to your computer and use it in GitHub Desktop.
Save xupyprmv/9322394 to your computer and use it in GitHub Desktop.
Automount a nonempty directory from /etc/fstab (bash, checked on Ubuntu 12.04)
#!/bin/sh
# Check parameter
if [ $# -eq 0 ]; then
echo "No argument specified. Right syntax is: automount.sh MOUNTPATH"
exit 1
else
# Check fstab contains folder
if cat /etc/fstab | grep $1 > /dev/null; then
# Check directory already mapped
if mountpoint $1 | grep 'is not a mountpoint' > /dev/null; then
# Move content into tmp folder if dir is not empty
if [ "$(ls -A $1)" ]; then
TDIR=`mktemp -d`.
mv "$1"/* $TDIR || exit 1
# Mount folder
mount $1 || true
# Copy content from tmp folder to mounted folder
mv "$TDIR"/* $1
else.
# Just mount folder
mount $1
fi
fi
else
echo "/etc/fstab not contains folder $1"
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment