djuarezg
djuarezg

Reputation: 2541

Run a command in loop until timeout, then export variable

I am trying to run a command in bash until it succeeds, but limit it with a timeout. The problem here is that I am using a subshell, therefore the main shell is unable to get the correct value:

timeout 10m bash -c 'until vm_ip=$( openstack server show f530d850-e255-4c5e-b984-43c4143a751b -c addresses --format value | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" ); do sleep 30; done'

With this I am trying to get the IP of a VM until it reaches a timeout. I would like to be able to use vm_ip after this command.

Upvotes: 2

Views: 216

Answers (1)

hidefromkgb
hidefromkgb

Reputation: 5903

Seems like it`s sufficient to just print the result to stdout and capture it to the variable in the target shell:

vm_ip="$( timeout 10m bash -c 'until vm_ip_internal=$( openstack server show f530d850-e255-4c5e-b984-43c4143a751b -c addresses --format value | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" ); do sleep 30; done; echo "$vm_ip_internal"' )"
echo "$vm_ip"

Upvotes: 2

Related Questions