Hi, folks.
I've done very little Bash scripting, and I'm trying to overcome my lazy past! I'd appreciate very much if you all would try out and analyze this script for me. It seems to work just fine. But I am not particularly pleased: while it does what it needs to do, it is not very elegant nor is it "tight."
Back in the day, I dabbled in some small programs in Basic, Pascal and C, and I'm just not getting the same feel of satisfaction with this script. OK, I realize this is not the same as a programming language -- even Basic-- but I get the feeling it's my ineptitude rather than the limitations of Bash scripting that is the issue here.
SCRIPT PURPOSE:
To generate subdirectories for mountpoints, based on user input.
BACKGROUND:
A number of the "fun" distros like Arch and some of the Slackware-derived distros have a terrible interface for adding mountpoints for additional data partitions at install time.
I'm a distro junkie: I have 2 HDDs with slots for 15 distros, and a total of 5 HDDs with (maybe) 60 partitions total. Some are root for a distro, some are data partitions I want accessible no matter what OS I'm booted into. Some months ago I wrote a different script that takes every "sdxnn" entry in the /dev directory and creates a corresponding directory in /mnt for a mountpoint.
Easy. But I want more!
THIS ONE:
I've long been peeved by the fact that partition sda1 is followed by sda11, sda12, etc. -- and then sda2, sda3, etc. come after sda15. The obvious solution for such a nit-picker is to make mountpoints that use double-digit numbering:
/mnt/sda01
/mnt/sda02 ...
/mnt/sda10
/mnt/sda11
/mnt/sda12
This script does that. But it's based on user input, rather than automatically polling the contents of /dev.
I've tried to trap the most obvious user errors, like entering the wrong letter for a drive or exceeding 15 partitions per drive --per the newer Linux kernel desire to make the whole world consist solely of serial/scsi drives. A bit of whimsy in the error output. . . forgive me. I am easily amused.
I rely pretty heavily on "case", with a few "if/then/elif/then" controls.
Does anyone see any obvious faults? Can anyone suggest a way to tighten up the algorithm?
Code:
#1 /bin/bash
# sbl-mountpoints2.sh
# creates mountpoints for HDD partitions based on user input.
# copyright SilverBear 2009 under GPL
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Contact: silverbear at sblinux dot org
# ##############################################################################################
clear # Clear the screen.
directory=${1-`pwd`}
echo "********************************************************************************************"
echo " You are now working in $directory"
echo "********************************************************************************************"
echo "Be CERTAIN this is the correct directory before you proceed! "
echo "HDD partition mountpoints will be created inside this directory."
echo "It should most likely be /mnt --so that you wind up with /mnt/sda01, /mnt/sda02, and so forth."
echo " "
# #################################
# Timeout_read for number input
TIMELIMIT=30 # 30 seconds
# #################################
# CHOOSING THE HARD DISK DRIVE
echo "This script will create mountpoints under /mnt for your additional data partitions."
echo "Choose a letter to create mountpoints for partitions on that drive. ** lOWER cASE, PLEASE! :-) **"
echo "sd[a]"
echo "sd[b]"
echo "sd[c]"
echo "sd[d]"
echo "sd[e]"
echo "sd[f]"
echo "sd[g]"
echo "sd[h]"
echo "-- or type [q] to quit"
echo "********************************************************************************************"
echo "You have about another 20 seconds to choose your HDD by letter. Hit ENTER key when finished"
echo "Hit the key combination *control-z* at any time to abort this script."
echo "********************************************************************************************"
echo " "
# get keyboard input
read -t $TIMELIMIT hdd <&1
# ^^^
# In this instance, "<&1" is needed for Bash 1.x and 2.x,
# but unnecessary for Bash 3.x and presumably Bash 4.x as well.
echo
if [ -z "$hdd" ] # Is null?
then
echo "Timed out, variable still unset."
exit 1
elif [[ "$hdd" != [a-h] ]]
then
echo "-------------------------------------------"
echo "You didn't choose a HDD between sda and sdh"
echo "-------------------------------------------"
echo "Script termination: user passed a bad variable."
echo "---------------------------------------------------------------------------------------------------"
echo "You know, many computer malfunctions occur in the interface between the chair and the keyboard."
echo " . . . loser."
echo
echo "Sorry. . . Just joking."
echo "I really admire you. I tend to mask my true feelings with insults. Why do you think that is?"
echo
echo
exit 1
else
echo "Making mountpoints for HDD $hdd"
fi
echo
#
case "$hdd" in
("a")
echo "We are making mountpoints for partitions on disk sda --right? (y/n --lower case)"
read ok
if [[ $ok == "n" ]]
then echo "Something is wrong, then."
echo "I quit. Restart the script and try it again."
echo " "
exit 1
else echo "OK! lets's continue. . ."
fi
;;
("b" )
echo "We are making mountpoints for partitions on disk sdb --right? (y/n --lower case)"
read ok
if [[ $ok == "n" ]]
then echo "Something is wrong, then."
echo "I quit. Restart the script and try it again."
echo " "
exit 1
else echo "OK! lets's continue. . ."
fi
;;
("c" )
echo "We are making mountpoints for partitions on disk sdc --right? ((y/n --lower case)"
read ok
if [[ $ok == "n" ]]
then echo "Something is wrong, then."
echo "I quit. Restart the script and try it again."
echo " "
exit 1
else echo "OK! lets's continue. . ."
fi
;;
("d" )
echo "We are making mountpoints for partitions on disk sdd --right? (y/n --lower case)"
read ok
if [[ $ok == "n" ]]
then echo "Something is wrong, then."
echo "I quit. Restart the script and try it again."
echo " "
exit 1
else echo "OK! lets's continue. . ."
fi
;;
("e" )
echo "We are making mountpoints for partitions on disk sde --right? (y/n --lower case)"
read ok
if [[ $ok == "n" ]]
then echo "Something is wrong, then."
echo "I quit. Restart the script and try it again."
echo " "
exit 1
else echo "OK! lets's continue. . ."
fi
;;
("f" )
echo "We are making mountpoints for partitions on disk sdf --right? ((y/n --lower case)"
read ok
if [[ $ok == "n" ]]
then echo "Something is wrong, then."
echo "I quit. Restart the script and try it again."
echo " "
exit 1
else echo "OK! lets's continue. . ."
fi
;;
("g" )
echo "We are making mountpoints for partitions on disk sdg --right? (y/n --lower case)"
read ok
if [[ $ok == "n" ]]
then echo "Something is wrong, then."
echo "I quit. Restart the script and try it again."
echo " "
exit 1
else echo "OK! lets's continue. . ."
fi
;;
("h" )
echo "We are making mountpoints for partitions on disk sdh --right? (y/n --lower case)"
read ok
if [[ $ok == "n" ]]
then echo "Something is wrong, then."
echo "I quit. Restart the script and try it again."
echo " "
exit 1
else echo "OK! lets's continue. . ."
fi
;;
("q")
echo "You chose to quit this script."
echo "Goodbye."
exit 0
esac
# ##################################################################################
# set number of partition mountpoints for a hard disk drive
echo
echo -n "How many partitions on disk sd${hdd}? ( type a number from 1 to 15)"
read -t $TIMELIMIT partitionz <&1
# ^^^
# In this instance, "<&1" is needed for Bash 1.x and 2.x,
# but unnecessary for Bash 3.x and presumably Bash 4.x as well.
echo
if [ -z "$partitionz" ] # Is null?
then
echo "Timed out, variable still unset."
exit 1
elif [ "$partitionz" -gt "15" ]
then
echo "---------------------------------------------------------------------------------------------------"
echo "Linux kernels above 2.6.20 only address 15 or fewer partitions per HDD. Start the script again."
echo "---------------------------------------------------------------------------------------------------"
echo "Script termination: user passed a bad variable."
echo "---------------------------------------------------------------------------------------------------"
echo "You know, many computer malfunctions occur in the interface between the chair and the keyboard."
echo " . . . loser."
echo
echo "Sorry. . . Just joking."
echo "I really admire you. I tend to mask my true feelings with insults. Why do you think that is?"
echo
echo
exit 1
else
echo "Number of partitions chosen = $partitionz"
fi
# set number of subdirectories for partitions
case "$partitionz" in
(1)
echo "You have 1 partition on sda."
echo "Changing single digit enumeration to double digits..."
mkdir sd${hdd}0$i
;;
(2)
echo "You have 2 partitions on sda."
for i in {1,2} ; do
echo "Changing single digit enumeration to double digits..."
mkdir sd${hdd}0$i
done
;;
(3)
echo "You have 3 partitions on sda."
# make directories with initial zero in name
for i in {1..3} ; do
echo "Changing single digit enumeration to double digits..."
mkdir sd${hdd}0$i
done
;;
(4)
echo "You have 4 partitions on sda."
# make directories with initial zero in name
for i in {1..4} ; do
echo "Changing single digit enumeration to double digits..."
mkdir sd${hdd}0$i
done
;;
(5)
echo "You have 5 partitions on sda."
# make directories with initial zero in name
for i in {1,2,3,5} ; do
echo "Changing single digit enumeration to double digits..."
mkdir sd${hdd}0$i
done
;;
(6)
echo "You have 6 partitions on sda."
# make directories with initial zero in name
for i in {1,2,3,5,6} ; do
echo "Changing single digit enumeration to double digits..."
mkdir sd${hdd}0$i
done
;;
(7)
echo "You have 7 partitions on sda."
# make directories with initial zero in name
for i in {1,2,3,5,6,7} ; do
echo "Changing single digit enumeration to double digits..."
mkdir sd${hdd}0$i
done
;;
(8)
echo "You have 8 partitions on sda."
# make directories with initial zero in name
for i in {1,2,3,5,6,7,8} ; do
echo "Changing single digit enumeration to double digits..."
mkdir sd${hdd}0$i
done
;;
(9)
echo "You have 9 partitions on sda."
# make directories with initial zero in name
for i in {1,2,3,5,6,7,8,9} ; do
echo "Changing single digit enumeration to double digits..."
mkdir sd${hdd}0$i
done
;;
(10)
echo "You have 10 partitions on sda."
# make directories with initial zero in name
for i in {1,2,3,5,6,7,8,9} ; do
echo "Changing single digit enumeration to double digits...making double digit partitions..."
mkdir sd${hdd}0$i
done
# make directories for partitions -ge 10
for i in {10} ; do
echo "making double digit partitions..."
mkdir sd${hdd}$i
done
;;
(11)
echo "You have 11 partitions on sda."
# make directories with initial zero in name
for i in {1,2,3,5,6,7,8,9} ; do
echo "Changing single digit enumeration to double digits...making double digit partitions..."
mkdir sd${hdd}0$i
done
# make directories for partitions -ge 10
for i in {10,11} ; do
echo "making double digit partitions..."
mkdir sd${hdd}$i
done
;;
(12)
echo "You have 12 partitions on sda."
# make directories with initial zero in name
for i in {1,2,3,5,6,7,8,9} ; do
echo "Changing single digit enumeration to double digits...making double digit partitions..."
mkdir sd${hdd}0$i
done
# make directories for partitions -ge 10
for i in {10..12} ; do
echo "making double digit partitions..."
mkdir sd${hdd}$i
done
;;
(13)
echo "You have 13 partitions on sda."
# make directories with initial zero in name
for i in {1,2,3,5,6,7,8,9} ; do
echo "Changing single digit enumeration to double digits...making double digit partitions..."
mkdir sd${hdd}0$i
done
# make directories for partitions -ge 10
for i in {10..13} ; do
echo "making double digit partitions..."
mkdir sd${hdd}$i
done
;;
(14)
echo "You have 14 partitions on sda."
# make directories with initial zero in name
for i in {1,2,3,5,6,7,8,9} ; do
echo "Changing single digit enumeration to double digits...making double digit partitions..."
mkdir sd${hdd}0$i
done
# make directories for partitions -ge 10
for i in {10..14} ; do
echo "making double digit partitions..."
mkdir sd${hdd}$i
done
;;
(15)
echo "You have 15 partitions on sda."
# make directories with initial zero in name
for i in {1,2,3,5,6,7,8,9} ; do
echo "Changing single digit enumeration to double digits...making double digit partitions..."
mkdir sd${hdd}0$i
done
# make directories for partitions -ge 10
for i in {10..15} ; do
echo "making double digit partitions..."
mkdir sd${hdd}$i
done
;;
esac
# ##################################
# End of script message
echo; echo " -- Done."
# #################################
echo "$partitionz mountpoints created in /mnt for sd$hdd partitions."
echo " Here you go:"
ls -l
# ##################################
# End of script message
echo
echo; echo " To add partition mountpoints for another HDD, run the script again."
echo " ___________________________________________________________________"
echo " Tip: When you hit the up-arrow key, you reload the last entered bash command."
echo " "
exit 0
thanks,
SilverBear