2

I'm trying to write a multi conditional check if statement, and i'm looking for an optimized way rather than the conventional one.

Assuming there are three variables (x, y & z) whose values keep changing over a period of time and are fetched from some unique files, if any of those variables value changes to zero, then we replace that variable with values from other pre-defined variables namely (a, b & c).

if you look at the first elif condition, you can see i had to write two levels of if elif. And i want to avoid so many lines of code. Any suggestions here.

a=1
b=2
c=3
x=`cat test.txt | grep 'assigned_value' | awk -F':' '{print$2}'`
y=`cat test1.txt | grep 'assigned_value' | awk -F':' '{print$2}'`
z=`cat test2.txt | grep 'assigned_value' | awk -F':' '{print$2}'`

if [[ $x -eq 0 && $y -eq 0 && $z -eq 0 ]]; then
    # execute something like below
    x=a
    y=b
    z=c
elif [[ $x -ne 0 ]]; then
    # check if y & z are equal to zero
    if [[ $y -eq 0 ]]; then
        # replace y with 'b' value
        y=b
        if [[ $z -eq 0 ]]; then
        # replace z with 'c' value
        z=c
        elif [[ $z -ne 0 ]]; then
        # that mean's variable x and z are already having some value and hence change is done only in variable y
        fi
    elif [[ $y -ne 0 ]]; then
        #check if z is equal to zero and replace its value
        if [[ $z -eq 0 ]]; then
        # replace z with 'c' value
        z=c
        elif [[ $z -ne 0 ]]; then
        # that mean's variable x and y are already having some value and hence change is done only in variable z
        fi
    fi
elif [[ similar check for variable y ]]; then
elif [[ similar check for variable z ]]; then
elif [[ $x -ne 0 && $y -ne 0 && $z -ne 0 ]]; then
    # do nothing as x, y & z already have some values associated
fi```

2 Answers 2

1

Computations of the variables x, y and z do not depend on each other, so you can proccess each one separately:

a=1
b=2
c=3

x=$(cat test.txt  | grep 'assigned_value' | awk -F':' '{print$2}')
y=$(cat test1.txt | grep 'assigned_value' | awk -F':' '{print$2}')
z=$(cat test2.txt | grep 'assigned_value' | awk -F':' '{print$2}')

if [[ $x -eq 0 ]] ; then
    x="$a"
fi

if [[ $y -eq 0 ]] ; then
    y="$b"
fi

if [[ $z -eq 0 ]] ; then
    z="$c"
fi
1

The logic that you describe is "if any of those variables value changes to zero, then we replace that variable with values from other pre-defined variables namely (a, b & c)".

This can be translated into simple bash statements:

(( $x )) || x="$a"
(( $y )) || y="$b"
(( $z )) || z="$c"

EDIT: Explanation:
In a boolean context the number 0 is regarded as false whereas numbers that are not equal to 0 are regarded as true.
|| is a logical or (often used to implement a failover) operator. What's right of the logical or is executed only if the left side resolves to false.
So you can interpret the 1st line as "check if $x is a number != 0, and if it's not then assign $a to it"

1
  • Can you explain what is going on here ? in the first parameter check what are we checking exactly ? Commented Jul 8, 2020 at 14:42

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