I’ve written small bash scripts before, but I bit off a little more this time, and I’m trying to program a little terminal game.

Anyway, I’ve run into a weird behavior. I have a function that, among other things, writes values to an array and returns either 0 or 1.

If I write echo "${name_of_array[@]}" inside the function, I see the contents of the array printed to standard output.

If I write echo "${name_of_array[@]}" outside the function immediately after executing the function, I see the contents of the array printed to standard output. (So, clearly the array is being treated as a global variable.)

But if I write the following, regardless of which value the function returns, nothing is printed to standard output but an empty line.

if ! name_of_function ; then
 echo "${name_of_array[@]}"
fi

Why is that the case? Doesn’t name_of_function get executed when evaluating the if statement? Is this some special case where all variables become local?

I realize I could just assign the function’s return value to a separate variable and then use that variable as the condition to the if statement, but it’s less elegant, and it doesn’t satisfy my curiosity. Is there any way to get the array out of the if statement alone?

It’s also possible I’m an idiot and my problem is just some random punctuation somewhere.

  • GiantRobotTRex@lemmy.sdf.org
    link
    fedilink
    English
    arrow-up
    2
    ·
    edit-2
    9 months ago

    This works fine for me:

    function foo() {
      bar[0]="hello"
      bar[1]="world"
      return 1
    }
    if ! foo; then
      echo "${bar[@]}"
    fi
    

    https://onlinegdb.com/xPIFP110w


    Are you getting messed up by the way bash handles exit statuses? An exit status of 0 indicates a success and a non-zero exit status indicates a failure (which allows for the different exit statuses to indicate different errors).

    So if my_func; then something; fi will only run something when my_func returns 0. In your case, you’re using ! to do the opposite so it only runs when your function returns a non-zero status.

    This can be quite surprising if you’re expecting the behavior found in other languages like python or C++ where 0 represents false and 1 represents true.

    • Ballig@programming.devOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      9 months ago

      I’ve never used pastebin or similar services, but if you feel like wading through my mess of a coding playground, the offending code is when findclosestbox calls either collisiondetection or findbetterport, and the debug “echo” I’m interested in is the one after “This is the end of the findclosestbox function:”

      https://zerobin.net/?9fa46f9871bb3607#tnEmm2JQZ3ggy2+fLp2oJwGf5r5uRcs0xj8zIvyurYQ=

      password: lemmydebug

    • Ballig@programming.devOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      9 months ago

      I went ahead and put the same command on either side of an if/else just to make sure I wasn’t messing up the return value. These results are interesting though; I suppose that means I just made an unrelated mistake somewhere. Incompetence is completely plausible in this case.

      Edit: Wait though, now my script works, but all I did was expand the if statement into name_of_function, return_variable=$?, and if ! [[ return_variable ]]; then. I didn’t screw anything else up. So the mystery remains.