BASH - Running functions in an array and avoiding spaghetti monster like code?
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 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 |
Personally I do not like the idea of attempting run something even if it is not there, ie the array items at the end run no matter what.
Here is an alternative you can play with ... please understand I am not saying yours is in anyway wrong: Code:
#!/bin/bash |
Don't want spaghetti?
Don't use Bash. You've got a dozen or more good programming languages at your disposal, and no one but you can tell what language(s) you used to write a script. No one, that is, except you, your team, and those who must follow in your footsteps after you (poor sot...) unexpectedly "failed the taxi test." Writing a real program? Use a real language. That is to say, one that was engineered to do the job. (P.S. Don't take my words personally. Not at all. I am expressing an engineering opinion, otherwise known as "Tools for the Job." I am expressing an experienced opinion about the (un-)suitability of the tools, not the tool users.) |
Its been a busy week. Thanks for all the replies folks I will look at them unfortunately later busy busy week.
thanks, dpc |
Consider getopts http://linux.die.net/man/1/bash or http://linux.die.net/man/1/getopt
|
All times are GMT -5. The time now is 11:50 AM. |