0

I am not sure if this was the proper place to post this, none-the-less: I am developing a script to negative match git branches, however when this part was run, it tried to get HTTP headers....can someone explain how this is happening?

test.sh

#!/bin/bash
array_not_contains()
{
    local array="$1[@]"
    local seeking=$2
    local in=0
    for element in "${!array}"; do
        echo $element #Commenting out this echo will stop it from fetching headers
        if [[ $element =~ $seeking ]]; then
            in=1
            break
        fi
    done
    return $in
}

exclude=() #array that will exclude the following matches from deletion
exclude+=(HEAD)
exclude+=(master)
exclude+=(develop)
exclude+=(example.*)


if $(array_not_contains exclude $1); then
    echo "win"
else
    echo "fail"
fi

Running it like this: ./test.sh bob will return headers

1 Answers1

4

It's ending up evaluating HEAD as a command.

bash$ type -all HEAD
HEAD is /usr/bin/HEAD

You probably misunderstand what if $(command) does. It runs command, then runs the output of that as a command, and examines its exit code.

The fix is easy: you mean

if array_contains exclude "$1"; then

though I would probably refactor the code to reduce its complexity more significantly.

As pointed out numerous times the if statement runs a command and examines its exit code.

bash$ flase () { echo "flase."; return 1; }

bash$ ture () { echo "ture."; return 0; }

bash$ if ture; then echo It is true.; else echo It is not.; fi
ture.
It is true.

bash$ if flase; then echo It is not false.; else echo It is false.; fi
flase.
It is false.
tripleee
  • 139,311
  • 24
  • 207
  • 268
  • Did you test this on macOS by any chance? `HEAD` isn't typically a command, but HFS+ is case-preserving, meaning it doesn't differentiate between `head` and `HEAD` as a file name, but keeps the case used in the call. – chepner Nov 28 '17 at 13:48
  • I linked the `HEAD` man page - it's a libwww-perl command which is still installed many places. – tripleee Nov 28 '17 at 13:48
  • Oops, sorry. Another reason to dislike HFS+'s default case-handling. – chepner Nov 28 '17 at 13:49
  • I am using an array to store regex match rules which will probably end up being fetched from somewhere outside the script, so reducing complexity is a bit hard(this is also a simplified snippet of code, not showing how the function is actually used). The point of having it run the array_not_contains function is to run the function and then evaluate its exit code(the echos are just for debugging purposes). Your fix does not allow the function to run, which means that the if does not fail on a positive match...defeating the whole point of the script – Al.UrBasebelon Tomeh Nov 28 '17 at 13:58
  • How does it not allow the function to run? Did you test?? The argument to `if` is a command line which is run by the shell. – tripleee Nov 28 '17 at 13:59
  • I did indeed test – Al.UrBasebelon Tomeh Nov 28 '17 at 14:01
  • Ah sorry, it does run, but it does not evaluate the return/exit of the function, which is what i need. i also tried [ ] but it didn't work. – Al.UrBasebelon Tomeh Nov 28 '17 at 14:07
  • Read slowly. The *purpose* of `if` is to run a command and examine its exit code. This is a FAQ. See e.g. the accepted answer on https://stackoverflow.com/questions/10646465/bash-simple-if-statement – tripleee Nov 28 '17 at 14:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160004/discussion-between-al-urbasebelon-tomeh-and-tripleee). – Al.UrBasebelon Tomeh Nov 28 '17 at 14:10