LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-10-2006, 11:39 AM   #1
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
Question bash script output on the same line


hello,
bash script outputs data continiously in terminal...
new data is printed on a new line
nothing unusual so far....

how to print new output in the place of previous (on the same line)?

Last edited by ygloo; 11-10-2006 at 11:51 AM.
 
Old 11-10-2006, 12:31 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Haven't used it that much myself, but take a look at tput.

It enables you to put string/text anywhere on the screen (and overwrite if something is already there), nice for building 'simple', but reasonably good looking menu's.

Hope this helps.
 
Old 11-10-2006, 12:38 PM   #3
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
if you want to print on the same line, you should never go to the second one
For example:

Code:
echo -n "This is the first line"
sleep 1
echo -e "\rsecond line on top of the first line!"
In the echo command the -n specifies that the echo should not add a newline at the end. And the -e specifies that there might be special escaped characters in the string (in this case there is a \r at the beginning which is the carriage return).

Cheers!
 
Old 11-10-2006, 01:34 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

@demon_vox: It does not overwrite the text that's already there, it puts the new text after the text already there. This makes the chance of line wrapping (by the terminal) greater.
 
Old 11-10-2006, 02:04 PM   #5
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi druuna, I test that code and it worked. The second echo starts writting at the begining of the line. Perhaps you missed the -e, in which case it would write after the text like you said.
The only problem that you might get is that the second echo might not cover up completely the output of the first one if the second string is shorter. But that can be fixed with spaces.

Be sure you copied the example exactly.
 
Old 11-10-2006, 02:25 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I did copy it exactly, there's another issue I did not mention. The ! at the end. Here's my output:
Code:
$ echo -n "This is the first line"
This is the first line$ sleep 1
$ echo -e "\rsecond line on top of the first line!"
-bash: !": event not found
$
It just hits me why it doesn't work (my way): Im doing this from the command line (which fails), not from within a script (which works nicely!).

I understand it not being on the same line if done from the command line, but the ! error puzzles me:
Code:
$ echo "second line on top of the first line!"
-bash: !": event not found

Ok, ! is a special character:
But escaping the ! has not the effect I expected:

$ echo "second line on top of the first line\!"
second line on top of the first line\!

Last edited by druuna; 11-10-2006 at 02:37 PM.
 
Old 11-10-2006, 02:36 PM   #7
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Original Poster
Rep: Reputation: 30
i try in this script, it won't work:

Code:
#!/bin/bash
# a clock

seconds=0
minutes=0
hours=0

sec ()
{
	while [ "$seconds" -le 59 ]; do
###
		echo -n "$hours " :"$minutes " ."$seconds "
		sleep 1
###
		echo -e "\r$hours " :"$minutes " ."$seconds "
		let "seconds += 1"
	done
	seconds=0 
	let "minutes += 1"
}

min ()
{
	while [ "$minutes" -le 59 ]; do
		sec
	done
	minutes=0 
	let "hours += 1"
}

hr ()
{
	while [ "$hours" -lt 24 ]; do
		min
	done
	hours=0 
}

while true; do
	hr	
done

Last edited by ygloo; 11-10-2006 at 02:40 PM.
 
Old 11-10-2006, 02:46 PM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I can't get it to work either with demon_vox's solution. It will work with tput (changes are in bold):
Code:
#!/bin/bash
# a clock
clear
seconds=0
minutes=0
hours=0

sec ()
{
     while [ "$seconds" -le 59 ]; do
          # place cursor at row 5 column 10
          tput cup 5 10
          echo "$hours " :"$minutes " ."$seconds "
          let "seconds += 1"
     done
     seconds=0
     let "minutes += 1"
}

min ()
{
     while [ "$minutes" -le 59 ]; do
          sec
     done
     minutes=0
     let "hours += 1"
}

hr ()
{
     while [ "$hours" -lt 24 ]; do
          min
     done
     hours=0
}

while true; do
     hr
done
 
Old 11-10-2006, 03:10 PM   #9
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Original Poster
Rep: Reputation: 30
found how to do it!!

echo -en "\r$hours :$minutes .$seconds "
 
Old 11-10-2006, 03:14 PM   #10
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi all,
the error with the ! is simple: just dont put that character in the string
Its not the same runnning it separately from a command line than to execute them all togheter from a script (and that's why it didnt worked for you at first).

In regards with the ygloo script, its not working because you are using my example too literal
The second echo always add a newline so you get your clock to write the time one line below the other. You should have used just one echo:

Code:
#!/bin/bash
# a clock

seconds=0
minutes=0
hours=0

sec ()
{
	while [ "$seconds" -le 59 ]; do
###
		echo -en "$hours " :"$minutes " ."$seconds "
		sleep 1
###
		let "seconds += 1"
	done
	seconds=0 
	let "minutes += 1"
}

min ()
{
	while [ "$minutes" -le 59 ]; do
		sec
	done
	minutes=0 
	let "hours += 1"
}

hr ()
{
	while [ "$hours" -lt 24 ]; do
		min
	done
	hours=0 
}

while true; do
	hr	
done
NOTE: I copied druuna idea of the modifications in bold! (good idea druuna! )

I think both ideas are good, each one has its strengths and disadvantages: my idea is simpler and it can be used easily in any language. But, it is quite limited since you cant do much more than that (and if you happen to output a newline somewhere else in your code, your output would look messy).
druuna idea's of using tput is probably more flexible, since tput will let you do more things (I learned today about this command, but having a flash look at the man page, it seems that it has more things you can do with the screen). On the other side, it is a bit more complex to use, and its not that easy to use in other language (not that it matters for this case, but its something I like to take into consideration )

In conclusion: we all learned new things today!

Cheers!
 
Old 11-10-2006, 03:33 PM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

And I found the solution to the echo "foo!" problem (because not using it is a workaround, not a solution ):
Code:
$ eval echo "foo\!"
foo!
If anybody knows why this echo "foo\!" results in foo\!, please let me know.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Grep's line numbers parsed into one line of output. judgex Programming 8 08-14-2006 04:22 AM
c++ console output: deleting a line Ictus Programming 3 03-29-2006 02:28 PM
Need help interpreting tcpdump output line wrw3 Linux - Networking 0 10-29-2005 07:47 PM
Command line to get output on a file satimis Programming 4 06-24-2005 06:04 AM
Command to output file content line by line aznluvsmc Programming 2 09-12-2004 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration