2

I am writing a script and according to that when I run it certain info will be displayed on the output screen.

for example say the constant data displayed is:

my name is mukesh.
i am 27 years old
unix version 7.2.3.0

but along with the above display something else is also need to be displayed(varying data) I.e

Process A is starting
Process A is running
Process A is completed.

but I don't want the above display.

I want Process A is starting to be cleared from screen and replaced by Process A is running and then by Process A is completed.

I am not very keen to use clear as it will remove the whole screen containing the constant data also. and also because the constant data takes a lot of time to process and to be displayed on the screen.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
munish
  • 7,825
  • 24
  • 71
  • 97

3 Answers3

3

The carriage return character (\r) will return to the beginning of the current line, so you can overwrite text:

printf "%s\r" "Process A is starting  "
sleep 5
printf "%s\r" "Process A is running   "
sleep 5
printf "%s\n" "Process A is completed."
glenn jackman
  • 84,176
  • 15
  • 116
  • 168
3

You can clear a line and use carriage return (\r) to get to the beginning of the line.

clr2eol=`tput el`                              # capture escape sequence for "clear-to-end-of-line"
echo -n "Process A has started."               # display without a newline
sleep 3
echo -n "\r${clr2eol}Process A is running."    # move to beginning of line (bol), clear, and display new text
sleep 5
echo -n "\r${clr2eol}Process A has completed." # again, move to bol, clear and display new test
echo                                           # terminating newline  you may not want to have this sent right away

Read the manpage on terminfo.

Arcege
  • 22,287
  • 5
  • 56
  • 64
0

To do something like that you'll have to use some like (n)curses: http://www.gnu.org/software/ncurses/

aseq
  • 454
  • 5
  • 13