Hi Folks,
I am writing an automation to share with a NAS community that is essentially an upgrade to my original version (which was line by line - it ran as you read; batch style with no functions or goto's). There is not specifically a need for this but I do it for fun of automating and streamlining my own setup of the NAS and pick up a thing or two occasionally that helps me out in my day job.
This new version will be about 1500 lines with about 20+ functions (or sequences as I like to call them - thats how I am currently using functions). I am looking to introduce command line argument(s) handling to the script. However the way I imagine the command line arguments would be used will be for NOT doing certain portions of the script. Essentially user runs the script: myscript.sh --no_orange --no_peach (this would do everything but peach and orange function sequences)
Basic Code (this is exact syntax I am using) looks something like this:
Code:
#!/bin/bash
intro () { echo "Start my fruit application installer"; }
installapple () { echo "Apple are installed"; }
installorange () { echo "Orange are installed"; }
installpeach () { echo "Peach is installed"; }
finished () { echo "Finished running this fruity automation"; }
runarray[1]=intro
runarray[2]=installapple
runarray[3]=installorange
runarray[4]=installpeach
runarray[5]=finished
for i in $*
do
case $i in
--no_apple)
#installapple is in index 2 of the array. Remove it and run the array
unset runarray[2]
echo "Skipping Apple"
;;
--no_orange)
#installorange is in index 3 of the array. Remove it and run the array
unset runarray[3]
echo "Skipping Orange"
;;
--no_peach)
#installpeach is in index 4 of the array. Remove it and run the array
unset runarray[4]
echo "Skipping Peach"
;;
--default)
echo "Nothing defined - All fruits will be installed"
;;
*)
echo "Did NOT recognize the command line arguements - No FRUITY APPS FOR YOU. Exiting"
exit
;;
esac
done
${runarray[0]}
${runarray[1]}
${runarray[2]}
${runarray[3]}
${runarray[4]}
${runarray[5]}
The questions I have today are:
A) Will this work?
I am not sure if I can just so easily put function commands into an array and just say "myarray" and that would run all the items in the array in order. Currently I am running each individual array index but was wonder if there was easy way to call them all using just one line (like ${runarray[all]}?
B) Is there a better way? I imagine could do an if statements if bash supports [switch] statements (thinking powershell/.net). If you think this way works just fine please share that too as its always good to hear. Please note when considering this question my scripting skills are modest at best and not sure if my modest brain can comprehend the really fancy stuff yet - looking for relatively simple alternatives if needed.
C) Is there a gaping hole in my logic? Common request I got about the old script was the option to NOT install certain applications/packages that my script would install. I thought it would be logical, considering the large number of applications/packages looking available to be installed, that I would make a subtractive handling vs mentioning each application/package user wanted.
appreciate all comments,
dpc