I have three separate bash scripts that send an email based on certain criteria in another script. What I would like to do is combine all those into one script and make them functions and use that in the main script but use flags to call the right function. So I have this code


    while getopts "f:s:k:" opt; do
        case ${opt} in
            f) sendFirstEmail 
            s) sendSecondEmail 
            k) sendKillEmail 


I didn't put the actual functions in that piece of code for readability but those functions each take an argument:$1. So what I want is to be able to do this: script.sh -f johndoe@mail.comand have that send johndoe the first email. When I run this, the code does nothing.


You need to parse the flag, which is a standard thing in scripting. You can use the Bash builtin getopts or the case construct. See man bash for details. I'd define all the functions first, then define a main funtion main and finally call it, passing it the flag(s).

You could also base the switch on the way the script is called and create differently named symbolic links to it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.