Pulling variables from functions executed as conditions

3 years ago by Ballig to c/shell

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.

DirigibleProtein 2 points 3 years ago

What is the return value ( $? ) from the function?

Not sure if if..then changes variable scope: it shouldn’t.

I’ve always used [[..]] around tests, not sure if that changes variable scope.

Where is the array first declared? Inside or outside the function?

Not at my computer at the moment, so I can’t give you a better answer. I’ll play with the idea later today.

path: 0 7928058, hotness: undefined, score: 2, children: 1
Ballig 1 point 3 years ago

The return value is either 0 or 1.

I don't know enough about coding to know what a variable's scope is. *SearXNG noises* You might be onto something. You think the function is being piped into a subshell?

Brackets didn't seem to help in my script; although maybe I should make a smaller test script just to rule out incompetence. (I'm guessing on the proper syntax for that, something like if ! [[ $(name_of_function "$arguments") ]] ; then, yes?)

I've tried both versions. name_of_array=() in or out of the function, also declare -a -g name_of_array, if I'm remembering those arguments right, at the top of the script.

Thanks for indulging my curiosity; I appreciate it.

path: 0 7928058 7928728, hotness: undefined, score: 1, children: 0
GiantRobotTRex 2 points 3 years 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.

path: 0 7928617, hotness: undefined, score: 2, children: 2
Ballig 1 point 3 years 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/...

password: lemmydebug

path: 0 7928617 7929638, hotness: undefined, score: 1, children: 0
Ballig 1 point 3 years 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.

path: 0 7928617 7928858, hotness: undefined, score: 1, children: 0
shell
shell

@programming.dev

login for more options
1615
50
1

From Ash, Bash and Csh to Xonsh, Ysh and Zsh; all shell languages are welcome here!

Rules:
  1. Follow Lemmy rules!
  2. Posts must relate to shell scripting. (See bottom of sidebar for more information.)
  3. Only make helpful replies to questions. This is not the place for low effort joke answers.
  4. No discussion about piracy or hacking.
  5. If you find a solution to your problem by other means, please take your time to write down the steps you used to solve your problem in the original post. You can potentially help others having the same problem!
  6. These rules will change as the community grows.

Keep posts about shell scripting! Here are some guidelines to help:

  • Allowed: Release Announcement of a command-line program designed for scripted use (e.g. bash, bats, awk, jq, coreutils/moreutils)
  • Allowed: Tutorials on using shell languages or supplementary tools designed for scripted use
  • Allowed: Code review/help requests for shell languages or supplementary tools designed for scripted use
  • NOT Allowed: Announcement of a CLI or TUI program that is not designed for scripted use (Yes, your fancy Zsh plugin which pretty-prints the date and time using only builtins is very cool, but unless you actually want to discuss the code itself, please check out !commandline instead!)
  • NOT Allowed: Domain-specific tutorials that do not involve shell scripting as a core component (e.g. docker-compose, ansible, nix). If you really love one of these, I'm sure there's a community out there ready to talk about them!
  • NOT Allowed: Code review requests for non-shell programming languages and configuration languages (e.g. Python, Yaml)

In general, if your submission text is primarily shell code, then it is welcome here!

go to feed...