-2
#!/bin/bash

START_SLEEP_TIME=2
LOOP_SLEEP_TIME=2


OK="false"
#need to wait for another proc first
sleep $START_SLEEP_TIME

RET=

check () {
  echo "---1--"
  echo "---2--"
  return 1
}
# isn't even called but just making sure it's not a syntax issue
finalize () {
        OK="true"
}


for i in {0..100}
do
        echo "check"
        VAL=check
        sleep $LOOP_SLEEP_TIME
done

echo "Checks terminated. $OK"

The script is supposed to do more stuff, but I am trying to strip it down to the essential to see why it is not working.

The above for me just always prints

check
check
check

after sleeping but it never prints --1-- or --2--. The function is supposed to be doing more stuff but if I can't even get this to work there's no point.

transient_loop
  • 5,984
  • 15
  • 58
  • 117
  • Oh I think I got a step further. `VAL=check` . If I just run `check` it works... – transient_loop Mar 04 '21 at 14:52
  • The point of this question was that I did initially not know what the problem was. If I knew it was related to how to set a variable to the output of a command I would have found that myself I guess. But nevermind editors, you can close it. – transient_loop Mar 04 '21 at 16:36
  • Part of the purpose of marking questions as duplicates is to help other people who run into similar problems (and are googling/etc for answers) to find the correct answer. It's not saying "you should've found this previous answer", it's saying "if anyone else finds this question because they ran into the same problem, here's where to find the answer." (Plus it avoids having different -- and maybe differently flawed -- answers to the same basic question/problem scattered all over the place. Put it in one place, and put pointers to it everywhere else it's relevant.) – Gordon Davisson Mar 04 '21 at 17:49

2 Answers2

1

VAL=check assigns the value check to VAL.

If you want to call the function check and assign the output from the function to VAL, make it:

VAL=$(check)

If you instead want the function's exit value, make it:

check
VAL=$?
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

Just call your check function, i.e. write is as a command. The returned value is then in $?.

Jens
  • 69,818
  • 15
  • 125
  • 179