Rewriting standard output lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rewriting standard output lines
# 1  
Old 09-28-2008
Rewriting standard output lines

Hello I'm curious about how to get a bash script to rewrite a line of standard output. For example, many programs track their progress by writing percentages on the screen:

Precent Done: 60%

That line gets updated periodically to reflect the status.. My question, is how do we do this, as opposed to writing to a new line every time you want to update the progress?
# 2  
Old 09-28-2008
Hi, You could try something like this... You may need to find a way to enable escape sequences in Your shell if it's not enabled by default. The trick here is to send BCKSPC with echo.

Code:
#!/bin/sh
echo Started program...
echo -n "Progress: 00" 
for x in $(seq -w 1 10); do # or some other loop condition
	sleep 1 #Or some other useful code here...
	echo -n "\b\b${x}"
done
echo

And You will have be wary of any other output produced by Your program, for example, if Your actions corresponding to sleep above actually writes stuff to stdout, You may want to redirect it temporarily to a file, which You can display after the loop is done.

/lakris
# 3  
Old 09-28-2008
Thanks lakris... The way to enable interpreting escape sequences is to use the -e option on echo, so your echo would look like:

Code:
echo -en "\b\b${x}"

And your script will work perfectly.

To be frank, I thought before of this possibilty (i.e. using backspaces), but I thought its rather "hackish" and painful to use in more general scenarios. Like you said, i'll have to be wary of following lines of output, so it will be rather difficult to implement this technique unless you're always writing to the last line. Isn't there a more generic or standard way of changing a line of output?
# 4  
Old 09-28-2008
Hi again,
well I can see several scenarios...
for example if You start a process that unzips a large file... start it in background with &, then if You loop over a condition (until $fsize -eq $expectedfsize, or something) You could regularly check for filesize (with wc -c maybe) and present the progress as a fraction of how much is done.
And when it comes to a terminal/tty, moving the cursor to where You want it is not hackish! It's how You do it, at least thats the way I see it Smilie

The are tools/libs such as ncurses that are made for easy manipulation of text screens. One just has to decide how much one need it and which is the path of least resistance.

/Lakris
# 5  
Old 09-28-2008
Hello Again Smilie

Quote:
Originally Posted by Lakris
And when it comes to a terminal/tty, moving the cursor to where You want it is not hackish! It's how You do it, at least thats the way I see it Smilie
But you're not simply moving the cursor, you're also erasing characters, thats why I thought its "hackish"..

Quote:
Originally Posted by Lakris
The are tools/libs such as ncurses that are made for easy manipulation of text screens. One just has to decide how much one need it and which is the path of least resistance.
/Lakris
Fair enough, maybe I'm asking too much from a pure bash script.. Thanks for replying!
# 6  
Old 09-28-2008
NP!
And Yes, bash and echo may seem a little limited in their abilities, but if You really want to go wild there's ANSI too!

I guess in a "real" program You would define an area for a function to write in so instead of deleteing the previous entry, one would overwrite it, but still You would have to make a lot of assumptions on screen size, ie rows/cols, and such.

/Lakris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SU and Standard Output

Bit of a strange one. Have a script called rapidclone_test.sh which calls Oracle rapidclone using su -c as an oracle osuser. However, if I control+c out to the calling shell anything entered is not displayed on the terminal. Any command executes successfully though. Why is the standard... (1 Reply)
Discussion started by: u20sr
1 Replies

2. UNIX for Dummies Questions & Answers

Capture Multiple Lines Into Variable As Of Standard Output

Hello All, I have the below script and output. cat test.sh #!/bin/bash -x logit() { echo " - ${*}" > ${LOG_FILE} } LOG_FILE=/home/infrmtca/bin/findtest.log VAR=`find . -type f -name "*sql"` logit $VAR Output: cat /home/infrmtca/bin/findtest.log -... (9 Replies)
Discussion started by: Ariean
9 Replies

3. Red Hat

Command understanding the output file destination in case of standard output!!!!!

I ran the following command. cat abc.c > abc.c I got message the following message from command cat: cat: abc.c : input file is same as the output file How the command came to know of the destination file name as the command is sending output to standard file. (3 Replies)
Discussion started by: ravisingh
3 Replies

4. Shell Programming and Scripting

How to execute standard output ?

Alright so i got this script genpipe: echo "$*" |sh genscript file vi file << 'HERE' :%s/^/echo /g :%s/ $//g :%s/ /&\| xargs \.\/plus /g :wq HERE cat file Which generates output like echo 1 | xargs ./plus 2 | xargs ./plus 3 and so on Now i got the next script multiplus, who should... (3 Replies)
Discussion started by: Bertieboy7
3 Replies

5. UNIX for Dummies Questions & Answers

Redirect Standard output and standard error into spreadsheet

Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway? Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
Discussion started by: killaram
6 Replies

6. Shell Programming and Scripting

Command Output to Standard Input

Hi All, How do I provide the output of a command to another command which is waiting for an input from the user ? Ex : I need to login to a device via telnet. In the script, initially I use the "read" command to get the IP Address, Username and Password of the device from the user. Now,... (1 Reply)
Discussion started by: sushant172
1 Replies

7. Shell Programming and Scripting

Standard output redirection from a variable

Hi, trying to store a comand involving a redirection in a variable and then run this variable. But the redirection gets lost. Ex: #!ksh MYCMD="ls -l > dirlist.txt" $MYCMD This runs the command but displays the result in the terminal instead of redirecting it to the text file. Can... (4 Replies)
Discussion started by: rm-r
4 Replies

8. UNIX for Dummies Questions & Answers

assigning variables from standard output

What am I doing wrong? I was searching for the answer to assigning variables from output. I found this simple response ls -l apply_want.m | read perms links owner group size mtime1 mtime2 mtime3 file this should allow me to echo the variables echo "$perms | $links | $owner | $group |... (2 Replies)
Discussion started by: whamchaxed
2 Replies

9. Shell Programming and Scripting

Standard output and redirection

Hello, Is is possible to redirect stdout to a file as well as to the console/screen or display in ksh. any thoughts suggestions/input is appreciated. Thanks. (2 Replies)
Discussion started by: jerardfjay
2 Replies

10. Shell Programming and Scripting

Standard Output

When I run a third parties program from the command line (this program basically list's a whole load of stuff) and write the output to a file it splits the output, i.e. in the middle of the file appears the exit command. If I don't redirect the output and write it to tty then the output is... (3 Replies)
Discussion started by: dbrundrett
3 Replies
Login or Register to Ask a Question