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?

terdon
  • 242,166
yael
  • 13,106

1 Answers1

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.

terdon
  • 242,166