Bash script, update output once command is ended

bash overwrite multiple lines
bash update last line
bash for loop
echo in the same line in shell script
bash echo refresh line
bash while loop
bash increment variable
bash clear line

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

command line, #!/bin/bash sek=60 echo "60 Seconds" while ((sek--)); do printf "One moment please: The dialog will remain on screen for the duration of the timer and update with the loop, and by the Press <OK> to finish" 10 25 Browse other questions tagged command-line bash scripts or ask your own question. 1. Use watch Command. Watch is a Linux command that allows you to execute a command or program periodically and also shows you output on the screen. This means that you will be able to see the program output in time. By default watch re-runs the command/program every 2 seconds. The interval can be easily changed to meet your requirements.

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 !!

How to Check if a Command Succeeded in Bash – Linux Hint, When you're scripting a series of commands and the previous output impacts the later, After running any command, bash will update the value of the variable. Another shell script which executes the previous one and redirects the log; I check the log file with the tail -f command for the success message. Even if I have exit 0 in the code I cannot end the tail -f process. Which doesn't let my script to finish. Is there any other way of doing this in Bash? The code looks like the following.

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

Mastering Bash, default all the changes you made to the output: once you modify a color or an attribute, everything Now, it is time to take advantage of what we just saw and modify the script we just at the end of the script we have this left on the command line: ${Yellow} "But we can use our color_print function to have a fancy output:  The most common use of the trap command though is to trap the bash-generated psuedo-signal named EXIT. Say, for example, that you have a script that creates a temporary file. Rather than deleting it at each place where you exit your script, you just put a trap command at the start of your script that deletes the file on exit:

Bash Scripting: Everything you need to know about Bash-shell , Last update: Tuesday, 10 September 2019, 3:12 AM | UTC A shell script is a fully-fledged programming language in itself. To execute multiple Bash commands and execute them at once, we need to save these commands in a file and execute that file In the above example, Hello World is the output in the terminal. # ===== # method 1A: returning a value from inline awk command via output stream $ cat expenses name date hotel breakfast lunch dinner beer Doug 2009-07-20 159 10 15 32 44 Doug 2009-07-21 159 0 12 25 0 Doug 2009-07-22 159 8 15 41 87 Doug 2009-07-23 159 0 0 10 0 Doug 2009-07-24 0 11 15 0 0 $ cat checker-inline-awk.sh #!/bin/bash # bash script

Linux All-in-One For Dummies, After you press Ctrl+D, the cat command ends, and you see the shell prompt again. Just as most Linux commands accept command-line options, a bash script simple one two to run the script, which displays the following output: This   A script can use this command to generate visible output to indicate the progress or results of the script as it is executed. 10. exit The exit command will close a terminal window, end the execution of a shell script, or log you out of an SSH remote access session.

sed, a stream editor, Details of sed scripts and individual sed commands are discussed in the next chapter. When the end of the file is reached, the temporary file is renamed to the output file's original Replace (change) lines with text (alternative syntax). d This command allows one to pipe input from a shell command into pattern space. One thing you can do is take an argument from the command line. So, for instance, when you run “script foo” the script will take the name of the first argument (foo): #!/bin/bash echo $1 Here bash will read the command line and echo (print) the first argument — that is, the first string after the command itself.

Comments
  • The echo "Let's check: " $locationOne is probably happening before command blabla finishes in background.
  • $locationOne once the result is ready - so output it to a file. Then print the file content.
  • @PaulHodges I tried to add a sleep command after last echo too but $locationOne is not displayed
  • @KamilCuk are you aware of any way to do it without a file?
  • First off, you need to return a value from your function. Secondly, you need to capture that in your main program. Thirdly, you need to use echo -n to leave the cursor at the end of the line, pending output from your function...
  • I can't change the order of the output I am afraid, also the script needs to be run on Mac OS X with a standard software configuration
  • Oh, I see what you mean now. You want the program to continue without waiting, and then when the output is ready, to go back to where the previous console output was and amend it on screen. That's significantly harder.
  • This is exactly what I mean @that other guy! The following script does what I want however it stops everything before printing last echo: #!/bin/bash sek=5 echo "$sek Seconds Wait!" while [ $sek -ge 1 ] do echo -ne "One Moment please $sek ... \r" sleep 1 sek=$[$sek-1] done echo echo "ready!"
  • Ok, I added an example
  • Your example works very well thank you! Everything works as it should however when I try to apply the same code in three different part of the same script the ouput gets messy as the cursor doesn't follow the location I gave it.
  • I have added & to allow the script to go ahead and don't wait for the results of checklocation
  • @Porcac1x Yes, but as your question says...you want to show it once it's ready... so you'll have to wait. Also locationOne is local to your function.
  • @MarkSetchell hey Mark, I'd like to show just LocationOne when the result is ready but everything (including Let's check) before the result is ready
  • This will invert the order though :(