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```
Aucun commentaire:
Enregistrer un commentaire