1

Im a little confused, I have a function that returns a exit code value and is also returning output, from running two expect scripts (I produce an exit code from the first script, and output from the second, in the function).

I am trying to figure out how use this function in a loop. I would like to loop the function until the exit code is zero, and then store the output value in a variable. Im a little stumped how to do this, I wonder if anyone can advise? My goal is:

run_expect_scripts()
{
    expect expect_script1
    exit_value="$?"
    output=$(expect expect_script2)
    echo "$output"
    return "$exit_value"
}


until [ run_expect_scripts -eq 0 -o a_counter -eq 5 ]; do
    a_counter=$(expr a_counter + 1)
    sleep 2s
done
use output of run_expect_scripts

Please how can I do this better?

1 Answer 1

1

One way of dealing with this is to use a temp file:

#!/bin/bash

myfunc() {
   echo "output $RANDOM"
   return $((RANDOM % 5))
}

tmp=$(mktemp)
until myfunc > "$tmp" || (( count++ == 5)); do :; done
cat "$tmp" 

Or if you don't want a temp file:

#!/bin/bash

myfunc() {
   echo "output $RANDOM"
   return $((RANDOM % 5))
}

ec=1
until [ $ec -eq 0 ] || (( count++ == 5)) 
do
  output=$(myfunc)
  ec=$?
done
echo "$output" 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.