1

My requirement is as follows,

if cond1 is true; then
    if [[ val1 -lt val2 ]]; then
        call_x_func
        call_y_func
        call_z_func
    fi
else
    if [[ val1 != val2 ]]; then
        call_x_func
        call_y_func
        call_z_func
    fi
fi

From above you can see that if cond1 is true then use operator -lt or else use !=. contents inside the loop remains the same. To acheive this, I am trying to do below but not able to assign bool value to bash variable. What would be the best way of doing this ?

need_change=false
if cond1; then
    need_change=[[ val1 -lt val2 ]]
else
    need_change=[[ val1 != val2 ]] 
fi

if $need_change; then
    call_x_func
    call_y_func
    call_z_func
fi
4
  • false in bash means unset. you can use ((need_change)) to check if it is true (or set)
    – user1986815
    Commented May 20, 2020 at 21:34
  • less than and not equals will always give you false, yes? at least in bash
    – Jetchisel
    Commented May 20, 2020 at 21:42
  • Also you're calling the same order of the functions regardless of the result of the test...
    – Jetchisel
    Commented May 20, 2020 at 21:50
  • You can't assign [[ ... ]] to a parameter; it's a command, not a value.
    – chepner
    Commented May 20, 2020 at 22:10

2 Answers 2

3

I often use "true" and "false" since they are also commands that merely return success and failure respectively. Then you can do

cond1=false
if "$cond1"; then ...fi

here what you looking for :

need_change=false
cond1=true
if "$cond1"; then
    if [[ val1 -lt val2 ]]; then need_change="true"; else need_change="false"; fi
else
    if [[ val1 -ne val2 ]]; then need_change="true"; else need_change="false"; fi
fi

if "$need_change"; then
    .
    .
fi
5
  • @Jetchisel hmm, he can emulate if/then/else logic with flag variables Commented May 20, 2020 at 22:22
  • @Jetchisel brother does it make sense now? Commented May 20, 2020 at 22:28
  • 1
    A && B || C is not "always wrong". One just needs to be aware of what happens if B exits non-zero. A variable assignment (not using command substitution) will have exit status zero. [[ condition ]] && var=foo || var=bar is perfectly fine. Commented May 20, 2020 at 23:30
  • Right, I might have used the wrong word, but to get used to && || because it works on this particular case will teach the script writer a bad habit, since there is always a catch, as opposed to using the if-statement, and this forum being viewed by thousand of people looking for a particular solution and most of the times code here ends up in production use.
    – Jetchisel
    Commented May 21, 2020 at 0:03
0

Since bash does not have a bool data type, I suggest that you model the bool by interpreting the numeric value 0 as true, and any other value as false. By doing this, you can easily use the exit code of programs as a boolean. For instance:

need_change=1  # set to false
((val1 < val2)) # Test the values
need_change=$? # Set to true or false, according to the outcome of the test
  # or:
need_change=$((val1 < val2)) # Alternative way to achieve this result.

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