mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
7.27k stars 345 forks source link

[ShellScripting] `While` bad format with condition "<=" #855

Closed DevDorrejo closed 2 years ago

DevDorrejo commented 2 years ago

In the next code

  while [ i <= "${ips_route[@]}" ]; do
        if [ $(grep nameserver /etc/resolv.conf | sed 's/^.* //') != ]; then
            true
        fi

when i do shfmt file.

will sort the while the next way

while [ i "${ips_route[@]}" ] <=; do
                if [ $(grep nameserver /etc/resolv.conf | sed 's/^.* //') != ]; then
                        true
                fi
        done

and will break the code.

using [[ ]] will stop this happen, but using a simple [] the format will brake the script.

thanks

mvdan commented 2 years ago

I don't think that code does what you think it does; [ does not have operators like <=, you are just redirecting stdin from a file called =, as in < =. You probably want [ foo -le bar ], for "less or equal".

DevDorrejo commented 2 years ago

yeah, was the "less or equal", i'm sorry for this.