0

I have 3 possible cases for my bash code.

I am creating a bash command, where I have 3 possible options, using flags I assigning to a local variable a value depending on the input parameters, my code depending on the input parameter generates a 1 kind of file, another kind of file or both files.

If to my command I add "-r" parameter I generate 1 kind of file, "red file" , local scripts var take, activeRed = 1

If to my command I add "-f" parameter I generate , "feed file" , activeFeed = 1

If in my command I use "-rf" I generate both files, activeFeed = 1 activeRed = 1

        if [[ $activeRed -eq 1 && $activeFeed -eq 1 ]]; then 
            python donwload_files.py --config config-ref-"$market".yml --path_arg $start
            python donwload_files.py --config config-feed-"$market".yml --path_arg $start

        elif [ $activeRed -eq 1 ];then
            python donwload_files.py --config config-ref-"$market".yml --path_arg $start

        elif [ $activeFeed -eq 1 ]; then
            python donwload_files.py --config config-feed-"$market".yml --path_arg $start

        else
            python donwload_files.py --config config-ref-"$market".yml --path_arg $start
            python donwload_files.py --config config-feed-"$market".yml --path_arg $start
        fi

As you can see I generate a very simply if-else statement, but I think there is a more efficient way for proccesing this 3 cases, in stead of this "non-visual" and repeat way.

Any idea about creating a more logical or efficient statement?

2
  • Is it $activeRed or $activeRef? Commented Sep 6, 2018 at 13:35
  • is ActiveRed, it was an error while copying it. Commented Sep 6, 2018 at 15:00

2 Answers 2

2

I assume that the else block is unwanted, because it wouldn't make sense then.

You can decrease the number of cases to only 2:

if [ $activeRef -eq 1 ];then
    python donwload_files.py --config config-ref-"$market".yml --path_arg $start

if [ $activeFeed -eq 1 ]; then
    python donwload_files.py --config config-feed-"$market".yml --path_arg $start

You want to run "config-ref" when $activeRef is equal to 1 and you want to run "config-feed" when $activeFeed is equal to 1, so there's no need to make more if-else cases

Sign up to request clarification or add additional context in comments.

Comments

1

It is easier. Just process each possibility independently:

if [ "$activeRef" -eq 1 ]
then 
    python donwload_files.py --config config-ref-"$market".yml --path_arg $start
fi
if [ "$activeFeed" -eq 1 ]
then
    python donwload_files.py --config config-feed-"$market".yml --path_arg $start
fi

For your else clause, when you set the activeRef and activeFeed vars, make sure that in the case where no parameters had been set, you must set both vars.

if [ "$activeFeed" -ne 1 && "$activeRef" -ne 1 ]
then
    activeFeed=1
    activeRef=1
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.