BashScripts.org
http://bashscripts.org/forum/

My first script
http://bashscripts.org/forum/viewtopic.php?f=7&t=314
Page 1 of 1

Author:  johngreenwood [ Sat Jan 06, 2007 1:21 am ]
Post subject:  My first script

Hi everyone, I'm getting into this scripting thing and here is my first script, Please let me know what you think, if I have done anything stupid or wrong.
Thanks


#! /bin/bash
#
#
# Mount or unmount specified drives to specified mountpoints
#

while getopts "VUh" Option
do
case $Option in
h) echo " "
echo "Mounting interactively mounts or umounts filesystems entered by user"
echo "Usage: amountscript [-VUh]"
echo "Options: -h print this help and exit"
echo " -V print version and exit"
echo " -U run script in unmount mode "
echo " "
exit;;
V) echo " "
echo "Mounting v0.3"
echo "Author J Greenwood"
echo "05/01/2007"
echo " "
exit;;
U) if
[ "$UID" -ne "0" ]
then
echo "Must be root to run this script"
exit
fi
echo "running Mounting in unmount mode"
YN=y
while [ "$YN" = "y" ]
do
echo -n "Enter device identifier (eg. /dev/hda1): "
read -e DEVICE
umount $DEVICE
echo -n "Unmount another? (y/n): "
read -e YN
if
[ "$YN" = "y" ]
then
echo ""
else
exit
fi
done
exit;;
esac
done


# Checks to see if you are root, if not you can't run the script
# and you will be told this in red

if
[ "$UID" -ne "0" ]
then
echo "This script must be run as root"
exit
fi

YN=y

# Keep looping until you say no to mount another
# Asks for information about the drive you want to mount
# And mounts it according to your answers

while [ "$YN" = "y" ]
do
echo -n "Enter device identifier (eg. /dev/hda1): "
read -e DEVICE
echo -n "Enter mountpoint (eg. /mnt/hda1): "
read -e MOUNTPOINT
echo -n "Would you like to mount $DEVICE read-only? (y/n): "
read -e RONLY
echo -n "Would you like to specify a filesystem type? (y/n): "
read -e FILETYPEYN


if
[ "$FILETYPEYN" = "y" ]
then
if
[ "$RONLY" = "y" ]
then
echo -n "Which filesystem does your device use? (eg. ext3): "
read -e FILETYPE
mount -t $FILETYPE $DEVICE $MOUNTPOINT -o ro
elif
[ "$FILETYPEYN" = "y" ]
then
echo -n "which filesystem does your device use? (eg. ext3): "
read -e FILETYPE
mount -t $FILETYPE $DEVICE $MOUNTPOINT
fi
fi

if
[ "$FILETYPEYN" = "n" ]
then
if
[ "$RONLY" = "y" ]
then
mount $DEVICE $MOUNTPOINT -o ro
elif
[ "$FILETYPEYN" = "n" ]
then
mount $DEVICE $MOUNTPOINT
fi
fi

# Asks if you want to mount another device

echo -n "Mount another? (y/n): "
read -e YN

if
[ "$YN" = "y" ]
then echo ""
else
exit
fi
done

exit

Author:  crouse [ Tue Jan 09, 2007 9:30 am ]
Post subject: 

Welcome to bashscripts johngreenwood ;)

Author:  crudball [ Wed Jan 17, 2007 4:06 pm ]
Post subject: 

Yup welcome :).
Woah thats your first script :o ?
Sure beats the hell out of mine :lol:

Author:  Buggin [ Tue Jan 23, 2007 4:26 pm ]
Post subject: 

my first was hello world

put it on a server and watched the boss cringe as i ran it (he didn't know what it was until after)

Author:  johngreenwood [ Sun Feb 18, 2007 8:43 pm ]
Post subject: 

It, took me about 2 days to write it, with the help of the Advanced Bash Scripting Guide: http://tldp.org/LDP/abs/html/

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com