Porcac1x
Porcac1x

Reputation: 145

Bash script, update output once command is ended

I have tried different ways but none of them have worked so far.

echo "Starting"

checklocation(){
if (command blabla)

then locationOne=$"Found"

else locationOne=$"Not found"

fi

}
checklocation &

echo "Let's check: " $locationOne

echo "Ending"

As my command take long time to provide the results I'd like to proceed to print all the output and show the value of $locationOne once the result is ready. The following code works fine printing all the output at once however the $locationOne doesn't appear. I tried with printf and \r too without luck. Any suggestions?

To clarify, I would like to load the variable value where the arrows are pointing once the command completes

Link to the image

Upvotes: 1

Views: 274

Answers (3)

that other guy
that other guy

Reputation: 123750

You want to go back and amend screen output later.

This is very difficult in the general case, but you can simplify it dramatically by making sure that the output you write doesn't scroll the screen in such a way that it's hard to predict where the amendment will have to be made.

This example does it by clearing the screen first, so that any extra output is unlikely to scroll. It can then update by coordinates:

#!/bin/bash

check() {
  sleep 3
  tput sc        # Save cursor
  tput cup 1 14  # Set y,x coordinates
  printf '%s' "Found"
  tput rc        # Restore cursor
}
check &
clear # Clear screen so we know where we are
echo "Starting"
echo "Let's check: "
echo "Ending"
wait

This shows:

Starting
Let's check:  
Ending

for three seconds, then it updates it to:

Starting
Let's check:  Found
Ending

Alternative approaches include:

  • Keeping the data you want on screen in a string, and just clearing+writing whenever you want to update the full screen
  • Tracking the number of lines you write, then using the "cursor up" ANSI command (tput cuu) to move up to where you believe the line to amend will be.

Upvotes: 0

P....
P....

Reputation: 18411

You can use wait to wait till child process status is changed. (check man wait)

#for demo puposes , I have manually added sleep of 10 sec. 
long_running_command()
{
sleep 10
echo "Hey, I am long running command....uh"
}

long_running_command &                 #<-using & to send this function to BG
echo "I am normal command"
wait                                   #waiting till the child is terminated. 

The above script will result in the following output:

I am normal command
Hey, I am long running command....uh

Upvotes: 0

Piyush
Piyush

Reputation: 838

echo "Starting"

checklocation(){
if (command blabla)
then
locationOne="Found"
else 
locationOne="Not found"
fi
}
echo "Calling function"
checklocation
echo "Let's check: " $locationOne
echo "Ending"

try following the above corrections,

Remove the "$" when assigning the locationOne variable

Also while calling the function remove "&", ignore this it is considered as an argument.

Goodluck !!

Upvotes: 1

Related Questions