Bash call function as array element












0















I'm in the process of designing a bash script to execute a number of functions in a sequence (installing packages, cloning repos, for instance) and after completion send a summary report to an email address to whether the installations succeeded or failed.



An idea that popped into my head was having an associative array and having the first element of a row a function call and the second any error code generated by the function. That way I can just loop through the array to call all the functions.



Something like-



function foo () {
//do something
return success
}

function bar () {
//do something
return success
}

declare -A arr
arr[0,0]=foo
arr[0,1]=1
arr[1,0]=bar
arr[1,1]=1

function init () {
for i in 0 .. 1 do
error = arr[i,0] //this im not sure about, would this execute foo()?
arr[i,1] = error
}

init


Is there a better way of doing this so that the script itself is easily modified for adding or removing functions as necessary?










share|improve this question

























  • Without seeing an example implementation of what you're thinking, it's hard to teel if something else may or may not be better. For example, I don't see why you need an associative array here, or how you plan to have a "row" in an array with two elements in it.

    – Olorin
    Mar 15 at 2:20











  • Sorry, edited for an example.

    – James McGrath
    Mar 15 at 3:29






  • 2





    I see. You can use two arrays instead of an associative array here. functions[0]=foo; errors[0]=1; .... 0,1 doesn't mean anything special in associative arrays, that's just the string 0,1. It's not like bash internally creates a row for 0 with columns labelled 1 and 0. What specifically is your concern about the script being "easily modified" here? You just need to add two lines for each function addition, so I'd call that easily modified.

    – Olorin
    Mar 15 at 4:42


















0















I'm in the process of designing a bash script to execute a number of functions in a sequence (installing packages, cloning repos, for instance) and after completion send a summary report to an email address to whether the installations succeeded or failed.



An idea that popped into my head was having an associative array and having the first element of a row a function call and the second any error code generated by the function. That way I can just loop through the array to call all the functions.



Something like-



function foo () {
//do something
return success
}

function bar () {
//do something
return success
}

declare -A arr
arr[0,0]=foo
arr[0,1]=1
arr[1,0]=bar
arr[1,1]=1

function init () {
for i in 0 .. 1 do
error = arr[i,0] //this im not sure about, would this execute foo()?
arr[i,1] = error
}

init


Is there a better way of doing this so that the script itself is easily modified for adding or removing functions as necessary?










share|improve this question

























  • Without seeing an example implementation of what you're thinking, it's hard to teel if something else may or may not be better. For example, I don't see why you need an associative array here, or how you plan to have a "row" in an array with two elements in it.

    – Olorin
    Mar 15 at 2:20











  • Sorry, edited for an example.

    – James McGrath
    Mar 15 at 3:29






  • 2





    I see. You can use two arrays instead of an associative array here. functions[0]=foo; errors[0]=1; .... 0,1 doesn't mean anything special in associative arrays, that's just the string 0,1. It's not like bash internally creates a row for 0 with columns labelled 1 and 0. What specifically is your concern about the script being "easily modified" here? You just need to add two lines for each function addition, so I'd call that easily modified.

    – Olorin
    Mar 15 at 4:42
















0












0








0








I'm in the process of designing a bash script to execute a number of functions in a sequence (installing packages, cloning repos, for instance) and after completion send a summary report to an email address to whether the installations succeeded or failed.



An idea that popped into my head was having an associative array and having the first element of a row a function call and the second any error code generated by the function. That way I can just loop through the array to call all the functions.



Something like-



function foo () {
//do something
return success
}

function bar () {
//do something
return success
}

declare -A arr
arr[0,0]=foo
arr[0,1]=1
arr[1,0]=bar
arr[1,1]=1

function init () {
for i in 0 .. 1 do
error = arr[i,0] //this im not sure about, would this execute foo()?
arr[i,1] = error
}

init


Is there a better way of doing this so that the script itself is easily modified for adding or removing functions as necessary?










share|improve this question
















I'm in the process of designing a bash script to execute a number of functions in a sequence (installing packages, cloning repos, for instance) and after completion send a summary report to an email address to whether the installations succeeded or failed.



An idea that popped into my head was having an associative array and having the first element of a row a function call and the second any error code generated by the function. That way I can just loop through the array to call all the functions.



Something like-



function foo () {
//do something
return success
}

function bar () {
//do something
return success
}

declare -A arr
arr[0,0]=foo
arr[0,1]=1
arr[1,0]=bar
arr[1,1]=1

function init () {
for i in 0 .. 1 do
error = arr[i,0] //this im not sure about, would this execute foo()?
arr[i,1] = error
}

init


Is there a better way of doing this so that the script itself is easily modified for adding or removing functions as necessary?







command-line bash scripts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 15 at 3:28







James McGrath

















asked Mar 15 at 0:50









James McGrathJames McGrath

11




11













  • Without seeing an example implementation of what you're thinking, it's hard to teel if something else may or may not be better. For example, I don't see why you need an associative array here, or how you plan to have a "row" in an array with two elements in it.

    – Olorin
    Mar 15 at 2:20











  • Sorry, edited for an example.

    – James McGrath
    Mar 15 at 3:29






  • 2





    I see. You can use two arrays instead of an associative array here. functions[0]=foo; errors[0]=1; .... 0,1 doesn't mean anything special in associative arrays, that's just the string 0,1. It's not like bash internally creates a row for 0 with columns labelled 1 and 0. What specifically is your concern about the script being "easily modified" here? You just need to add two lines for each function addition, so I'd call that easily modified.

    – Olorin
    Mar 15 at 4:42





















  • Without seeing an example implementation of what you're thinking, it's hard to teel if something else may or may not be better. For example, I don't see why you need an associative array here, or how you plan to have a "row" in an array with two elements in it.

    – Olorin
    Mar 15 at 2:20











  • Sorry, edited for an example.

    – James McGrath
    Mar 15 at 3:29






  • 2





    I see. You can use two arrays instead of an associative array here. functions[0]=foo; errors[0]=1; .... 0,1 doesn't mean anything special in associative arrays, that's just the string 0,1. It's not like bash internally creates a row for 0 with columns labelled 1 and 0. What specifically is your concern about the script being "easily modified" here? You just need to add two lines for each function addition, so I'd call that easily modified.

    – Olorin
    Mar 15 at 4:42



















Without seeing an example implementation of what you're thinking, it's hard to teel if something else may or may not be better. For example, I don't see why you need an associative array here, or how you plan to have a "row" in an array with two elements in it.

– Olorin
Mar 15 at 2:20





Without seeing an example implementation of what you're thinking, it's hard to teel if something else may or may not be better. For example, I don't see why you need an associative array here, or how you plan to have a "row" in an array with two elements in it.

– Olorin
Mar 15 at 2:20













Sorry, edited for an example.

– James McGrath
Mar 15 at 3:29





Sorry, edited for an example.

– James McGrath
Mar 15 at 3:29




2




2





I see. You can use two arrays instead of an associative array here. functions[0]=foo; errors[0]=1; .... 0,1 doesn't mean anything special in associative arrays, that's just the string 0,1. It's not like bash internally creates a row for 0 with columns labelled 1 and 0. What specifically is your concern about the script being "easily modified" here? You just need to add two lines for each function addition, so I'd call that easily modified.

– Olorin
Mar 15 at 4:42







I see. You can use two arrays instead of an associative array here. functions[0]=foo; errors[0]=1; .... 0,1 doesn't mean anything special in associative arrays, that's just the string 0,1. It's not like bash internally creates a row for 0 with columns labelled 1 and 0. What specifically is your concern about the script being "easily modified" here? You just need to add two lines for each function addition, so I'd call that easily modified.

– Olorin
Mar 15 at 4:42












0






active

oldest

votes











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1125786%2fbash-call-function-as-array-element%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1125786%2fbash-call-function-as-array-element%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How did Captain America manage to do this?

迪纳利

南乌拉尔铁路局