Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display and refresh multiple lines in bash

Tags:

bash

I am writing a installation script and would like to display the status of the script as it progresses.

example:

var1="pending"
var2="pending"
var3="pending"

print_status () {
echo "Status of Item 1 is: "$var1""
echo "Status of Item 2 is: "$var2""
echo "Status of Item 3 is: "$var3""
}

code that does something and then refreshes the
output above as the status of each variable changes.
like image 847
user2704404 Avatar asked Aug 21 '13 16:08

user2704404


People also ask

How do I echo multiple lines in bash?

To add multiple lines to a file with echo, use the -e option and separate each line with \n. When you use the -e option, it tells echo to evaluate backslash characters such as \n for new line. If you cat the file, you will realize that each entry is added on a new line immediately after the existing content.

How do I print multiple lines in Linux?

First, the line containing the pattern /Linux/ is found. The command within the braces will run on the pattern found. {N;p} means read the next line and print the pattern space which now contains the current and the next line. Similarly, to print 2 lines, you can simply put: {N;N;p}.

What does %% mean in bash?

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.

How do I print a new line in bash?

Printing Newline in Bash The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way. However, it's also possible to denote newlines using the “$” sign.


2 Answers

This code should give you the idea:

while :; do
    echo "$RANDOM"
    echo "$RANDOM"
    echo "$RANDOM"
    sleep 0.2
    tput cuu1 # move cursor up by one line
    tput el # clear the line
    tput cuu1
    tput el
    tput cuu1
    tput el
done

Use man tput for more info. To see the list of capabilities use man terminfo

like image 166
Aleks-Daniel Jakimenko-A. Avatar answered Sep 27 '22 20:09

Aleks-Daniel Jakimenko-A.


Look at this:

while true; do echo -ne "`date`\r"; done

and this:

declare arr=(
  ">...."
  ".>..."
  "..>.."
  "...>."
  "....>"
)

for i in ${arr[@]}
do
  echo -ne "${i}\r"
  sleep 0.1
done
like image 20
rook Avatar answered Sep 27 '22 21:09

rook