Updating the last two lines displayed on the terminal using the shell?

I am trying to create a simple progress bar script that will display a panel as well as the current activity.

With help echo -ne "[||...] Processing ${file_name}\r"

I can display the progress bar like:

[|||||||| ............] Processing file1

However, I expect to output something like this:

File processing1

[|||||||| ............]

Note. Both are separate lines and I want to update them as my progress script.

Is there a way to achieve this?

+3


source to share


2 answers


You can use tput cuu 2

to move the cursor up two lines and tput el

to clear the current line:

while sleep 1
do
    tput el; date
    tput el; echo "$RANDOM"
    tput cuu 2
done

      



This will create two mutable lines, for example

Thu Jul 23 17:50:46 PDT 2015
23676  

      

+4


source


The solution suggested by @that another guy suggests that to fill in the differences in $RANDOM

(a non-standard bash -special function that returns 1 to 5 digits).

This can be accomplished using a terminal capability that clears from the cursor position to the end of the line. In both cases, the scripts assume the messages are short enough that line wrapping is not an issue (the OP's question gave an example with this capability):



#!/bin/bash
UP1="$(tput cuu 1)"
EL0="$(tput el)"
while sleep 1
do
    date
    printf '%s%s\r%s' "$RANDOM" "$EL0" "$UP1"
done

      

+1


source







All Articles