Suppose, you create a script that does N automated tasks. However, you want to create it in such a way, that it runs 1 to N number of tasks, depending on one of the CLI argument (decimal equivalent of binary number of N digits).

# Scenario

Let us take N=3, so the decimal equivalent of min-max binary numbers would be 0 and 7. So, assuming the automated tasks are A, B, C. So, here’s binary notation for A, B, C and their equivalent decimal numbers.

A B C 0 0 0 = 0 0 0 1 = 1 0 1 0 = 2 0 1 1 = 3 1 0 0 = 4 1 0 1 = 5 1 1 0 = 6 1 1 1 = 7

So, in the above tabular data,

**ONE**denotes that task**A**should run, whereas**ZERO**denotes that the tasks shouldn’t run. For instance,**FIVE**denotes that**A**and**C**will run.# BNRY_CMB contains the value obtained from CLI argument.. flag=$BNRY_CMB # Start infinite loop.. while :; do case $BNRY_CMB in 1) # Run Task 'A' case $flag in 1|3|5|7) break;; esac ;; 2|3) # Run Task 'B' case $flag in 2|6) break;; 3|7) BNRY_CMB=1; continue;; esac ;; 4|5|6|7) # Run Task 'C' case $flag in 4) break;; 5) BNRY_CMB=1; continue;; 6|7) BNRY_CMB=2; continue;; esac ;; esac done

Smartly, making use of case-esac construct and infinite loop, you can handle all the automated tasks (A and/or B and/or C). Well, this scenario helped me understand the basics of case-esac and while-loop constructs, easily.

Hope this helps 😉

Advertisements