0

I have a main script, containing multiple child script inside it. once i will run the main script, it will start execution of all child script sequentially. some script will pass and some could be fail. Second time when i run this main script, i want only the failure child script should run not the successful one. Any help will be really appreciated.

My second issue is, there is one argument say 'r' is common for all script..while 'd' is optional. eg: mainscript.sh -r India -d 2021-05-21 but my first child script doesn't have d param, so it is failing in every run. How can i ignore this invalid param and run the script.

#!/bin/bash

recall() {
script1.sh $* ;
script2.sh $* ;
script3.sh $* ;
script4.sh $* ;
script5.sh $* 
exit
}
recall $*
4
  • 2
    for that optional arg, you'll need to change the script to recognize it as a valid argument, even if all you do is just to ignore it. In general, use "$@" (with quotes) instead of $*, it deals better with arguments containing whitespace or glob characters
    – ilkkachu
    Sep 3, 2021 at 9:18
  • @ilkkachu Thank u for this good suggestion.
    – Rims
    Sep 3, 2021 at 9:52
  • How often would you need to (re-)run the script. When should the "succeeded last time" status "expire"? After the second run, or after a certain timespan, or after yet another condition (e.g. some result file appeared)?
    – AdminBee
    Sep 3, 2021 at 10:33
  • @AdminBee There is no any time duration defined, it could be multiple time. Actually it depends on user, Say user run main script and noticed one of child script failed..return status is 1, He will fix the issue internally and run the main script again. success script will return 0 as a status and 1 for failure.
    – Rims
    Sep 3, 2021 at 10:58

1 Answer 1

1

The usual approach for ensuring that a script (or any program really) can be run only once is to create a flag somewhere that maintains state.

For the purposes of this example, let's take the existence of a file in the user's home directory as our marker: if the file exists then the application has been run successfully and must not be run again.

#!/bin/bash
flag="$HOME/.my_script.flg"
{[ ! -f "$flag" ]] && my_script && touch "$flag"

Here, we test for the the presence of a $flag file. If it does not exist we run an external script my_script, and provided it exits with success/0 status we create the $flag file. Notice that there is a race condition: if you run two instances of this control script simultaneously you could end up with the first script not creating the flag file until the second had already started running the same child script. I'm going to ignore this issue on the basis that your user should know what they're doing, and if they don't then you need to look at ensuring only a single instance of the script can be run simultaneously.

This can be extended for your specific situation:

#!/bin/bash
recall() {
    local script flag
    for script in script{1,2,3,4,5}.sh
    do
        flag="$HOME/.${script%.sh}.flg"
        [[ ! -f "$flag" ]] && "$script" "$@" && touch "$flag"
    done
    exit 0
}
recall "$@"

Here, the $script variable is used to iterate across the names of the child scripts or functions you want to execute. We assume each one returns 0 for success and non-zero otherwise.

If you want the overall exit status to be 0 on success and non-zero on any failure, you would need to extend the script somewhat (we return the number of modules that failed, regardless of the number that were run):

#!/bin/bash
recall() {
    local script flag error=0
    for script in script{1,2,3,4,5}.sh
    do
        flag="$HOME/.${script%.sh}.flg"
        if [[ ! -f "$flag" ]]
        then
            if "$script" "$@"
            then
                touch "$flag"
            else
                ((error++))
            fi
        fi
    done
    return $error
}
recall "$@"

Needless to say, remove the appropriate flag marker file(s) to allow the corresponding child script to be re-run.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .