Score:2

How can I print text while downloading file in ubuntu?

ru flag

I'm currently writing a Bash script. I want to download a file while printing text. For example, consider this script:

echo -e "---------------------------"
echo -e "Your file downloading..." 
echo -e "---------------------------"
wget example.com/1gbfile

In the second echo each . should be printed successively every second till the download finishes. If the number of . becomes three, like this: ..., it should be reset to only one . and continue the loop.

Artur Meinild avatar
vn flag
So the built-in indicator by `wget` doesn't do the job for you?
Artur Meinild avatar
vn flag
I think the OP is actually interested in creating a "spinner like" animation, like this: https://askubuntu.com/questions/623933/how-to-create-a-rotation-animation-using-shell-script/
cn flag
Also https://stackoverflow.com/q/73723689/7552
Erikli avatar
ru flag
@ArturMeinild Yea, we can call it like that. I'm gonna obfuscate my script because of some private things like download server. And wget shows the download server etc. So I wanted to make something cool like this.
Score:4
vn flag
Main script:

Create the following script as the source for your progress/bouncing bar (I called it bash-progress):

#!/bin/bash

# Initial configuration variables

# Set time interval for progress delay (in fraction of seconds)
time_delay=".2"

# Set left and right brackets (1 character)
  lb="["
#  lb="("
#  lb=" "
  rb="]"
#  rb=")"
#  rb=" "

# Function to show bouncing bar while running command in the background
show_bouncer() {
  # If no argument is given, then this is run on the last command - else provide PID
  if [[ -z $1 ]]
  then
    PID=$!
  else
    PID=$1
  fi
  ii=0
  # Define bouncer array (3 characters)
  bo=('.  ' '.. ' '...' ' ..' '  .' ' ..' '...' '.. ')
#  bo=('⠄  ' '⠂⠄ ' '⠁⠂⠄' '⠂⠂⠂' '⠄⠂⠁' ' ⠄⠂' '  ⠄' '   ')
#  bo=('⡇  ' '⣿  ' '⣿⡇ ' '⢸⣿ ' ' ⣿⡇' ' ⢸⡇' '  ⡇' '   ')
  # True while the original command is running
  while [[ -d "/proc/$PID" ]]
  do
    ch="${bo[(ii++)%${#bo[@]}]}"
    printf "%b" " ${lb}${ch}${rb}"
    sleep "$time_delay"
    # Adjust backspaces to bouncer length + 3
    printf "\b\b\b\b\b\b"
  done
}

The script can work in 2 ways: Either by using the PID of the last command run, or with a given PID. The most common use is with the last command.

Using it:

So you simply create your other script like this:

#!/bin/bash

# Include Bash progress bars - or include the entire source in your script.
source "./bash-progress"

your_command_here &
show_bouncer

It's important to run the command in the background, since it then moves on immediately to show the bouncer.

You can easily test it with a sleep command:

#!/bin/bash

# Include Bash progress bars - or include the entire source in your script.
source "./bash-progress"

sleep 5 &
show_bouncer
Bonus info:

To use with a PID other than the last one, you can use pgrep (-n for newest and -x for exact match) to find the latest instance of the process like this:

#!/bin/bash

# Include Bash progress bars - or include the entire source in your script.
source "./bash-progress"

your_command &
do_something_else
do_anything_meanwhile
show_bouncer $(pgrep -nx "your_command")
BeastOfCaerbannog avatar
ca flag
Really nice, simple and customizable script!
Artur Meinild avatar
vn flag
Thanks. I have a slew of other variations, I think I'll put it on Gitlab when I get around to it, and link here.
Erikli avatar
ru flag
It works perfectly fine. Thank you so much! That's what I was looking for exactly!!
Erikli avatar
ru flag
@ArturMeinild Umm, I'm sorry. I don't want to be sassy but how can I use it for the second echo? I tried to implement it but i screwed it up :/
Artur Meinild avatar
vn flag
I'm sorry, but if you want to implement it like that (where you're going to go back and redraw a specific area of the screen where you have already printed), then that's something you'll have to research yourself. My implementation is more "linear", so that the bouncing bar is the last thing that's printed while it waits.
Artur Meinild avatar
vn flag
In that case, you need to modify the print function inside the `while` loop to print exactly what you like. But you need to figure that out yourself.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.