seemvision
seemvision

Reputation: 242

Bash - run when function returns a value

I have a simple function that calls and endpoint to get a value:

get_token() { curl -sf "http://127.0.0.1:8080/v1/$1?raw"; }

And when you run:

echo $(get_token root)

It returns a token as a string. Now I want to execute some commands only once, and when this function returns a value because I don't know when the API is up to return the value (almost asynchronous). I thought I can do something like:

until [ ! $(get_token root)]; do echo "Hello World!"; done

But this runs the echo command infinitely when that function returns the value. Is there a better way to do this?

Upvotes: 0

Views: 49

Answers (2)

jeremysprofile
jeremysprofile

Reputation: 11504

so... you want to wait until you get a value, and then echo something once?

while [[ -z $token ]]; do
    token=$(get_token root)
done
echo "Hello World!"

Upvotes: 2

Ipor Sircer
Ipor Sircer

Reputation: 3141

while [ -z "$token" ]; do
    echo "Hello World!"
    token=$(get_token root)
    sleep 1
done

Upvotes: -1

Related Questions