LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-25-2011, 03:17 AM   #1
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Rep: Reputation: 15
Question 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

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

Last edited by Dimitriy; 08-25-2011 at 04:18 AM.
 
Old 08-25-2011, 05:02 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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

switch_reg='--no_(apple|orange|peach)'

for args
do
    if [[ ! "$args" =~ $switch_reg ]]
    then
        echo "Inavalid switch :- $args. Please start again"
        exit
    fi
done

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"; }

intro

for j in install{apple,orange,peach}
do
    if [[ $@ =~ --no_${j#install} ]]
    then
        echo "Skipping ${j#install}"
    else
        $j
    fi
done

finished
I have also heard that it is frowned upon to run commands stored in variables, but I believe it is a personal choice.
 
Old 08-25-2011, 09:24 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.)

Last edited by sundialsvcs; 08-25-2011 at 09:26 AM.
 
Old 08-31-2011, 01:10 PM   #4
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
Its been a busy week. Thanks for all the replies folks I will look at them unfortunately later busy busy week.

thanks,
dpc
 
Old 08-31-2011, 08:53 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752
Consider getopts http://linux.die.net/man/1/bash or http://linux.die.net/man/1/getopt
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
Running functions in bash script as another user with su pwc101 Programming 5 12-13-2008 09:19 AM
Running commands from array in a child bash script bengoavs Programming 2 10-26-2007 02:16 PM
running functions on the same time (bash programming) sharapchi Programming 1 11-12-2006 11:55 AM
array functions in c djgerbavore Programming 6 01-07-2005 03:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration