Thursday, September 22, 2011

Two echo commands on the same line

I want to:
echo two or more lines output inside a loop such that the loop outputs the echo on the same lines, to prevent scrolling when the loop is for example 100 times.

I searched the web for "shell echo output same line", but didn't find what I wanted. I got this command
echo -en "hello"
which the -en option means print on the same line from the first character place. Which did not do the trick which I wanted because it is only for one line. Then I found the command "tput" which manipulates the cursor, and the code that moves the cursor up 2 lines
echo -en "\033[2A"
and here is what I got:

A sample code for echo 'ing two lines on the same place! It can be used to print more than two lines on the same lines.

echo "Total Files to search " $count 
echo "" # These two empty lines are needed for proper formating.
echo "" # For example when the cursor is at the end of the screen!
echo -en "\033[2A" # Move the cursor up 2 lines
tput sc	# Save the cursor position

j=0
for i in $searchDir
do
	tput el	# clear to end of line
	echo "Searching count $j out of $count"
	tput el # clear to end of line
	echo "Searching file $i"
	tput rc # Restore the cursor position

	j=`expr $j + 1`
done

echo ""
echo ""
echo ""
echo "Finished"

Thats all.

No comments:

Post a Comment