3

in my bash code sometimes I use long parameters in Comparison Operators to test the "OR"

as the following:

if [[ $production_check_linux_disks_quantity == false  ]] || [[ $production_linux_mount_point_reference_to_ambari_definition == false ]] || [[ $production_check_linux_mount_point_quantity == false ]] || [[ $production_show_linux_disks_simulation == false ]]
then

problem is that syntax is too long and ugly

some idea how to create it shorter and elegant?

  • I would recommend to not use such long variable names – RomanPerekhrest Dec 28 '17 at 8:04
2

You can break the line on the ||. In fact, you can break the line at any control operator. So, at any of ||, !, &&, &, ;, ;;, |, |&, (, or ).

This is correct syntax for your example, for instance, there's no need for \:

if [[ $production_check_linux_disks_quantity == false  ]] || 
   [[ $production_linux_mount_point_reference_to_ambari_definition == false ]] || 
   [[ $production_check_linux_mount_point_quantity == false ]] || 
   [[ $production_show_linux_disks_simulation == false ]]; then
    echo yes
fi

You do need \ to connect multiple lines if they don't end with a control operator, but not when they do.

Also, note that if [[ $var == false ]] is checking whether the value of the variable $var is the string false, and is better written as if [[ $var = 'false' ]]. Your code suggests that you might be expecting it to evaluate to true if $var is -1 or 0 or unset or something.

  • dose - && work the same as || ?? – yael Dec 28 '17 at 12:25
  • @yael yes, all of the operators I list in the second sentence can end lines. – terdon Dec 28 '17 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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