Home > Software engineering >  How to echo string while waiting for variable assignment from another command?
How to echo string while waiting for variable assignment from another command?

Time:04-22

I'm using CLI in which some commands can take few minutes. After it is run, it echos 'in progress' every 10 seconds, until it's done. Example command:

cli run:command --json

Example output:

in progress
in progress
in progress
{"status":"0", "result":"very good"}

I need to assign output json to variable. I do it like this:

var=$(cli run:command --json)

My output is good, it contains only json, without 'in progress' lines. But when I'm waiting for command to be done I don't see 'in progress' status every 10 seconds.

How I can assign output to variable, but still see 'in progress' status when I'm waiting for it?

CodePudding user response:

Here's one method: launch an infinite loop to print 'in progress' in the background, and when the "important" command completes, kill the background process:

(while true; do echo in progress; sleep 1; done) & 
bgpid=$!

var=$(sleep 10; echo done)     # this is your long-running command

kill $bgpid
  • Related