0

[Answered] The method that I went with that works and is beginner friendly, I simply added the

# Progress Bar

echo -n 'Progress: [--------------------]'
sleep 1     # <- Command 1
echo -ne '\rProgress: [#####---------------]'
sleep 1     # <- Command 2
echo -ne '\rProgress: [##########----------]'
sleep 1     # <- Command 3
echo -ne '\rProgress: [###############-----]'
sleep 1     # <- Command 4
echo -e '\rProgress: [####################]'

right before the print the end status message and it works like a charm for this situation; testing purposes and getting a feel for it. Hopefully, this helps anyone else in the future.


I've looked over the forums and posts but it seems as though most of them include having to install additional addons to inject a progress bar. I'm new to scripting and trying to learn and thought this would be a great addition to a simple script.

I understand how to use pv to add a progress update but it requires installation before it would work. Which is completely opposite of what I'm wanting.

Is there not a way to use the same progress bar that's built into linux when updating your system or installing applications like in the image below (for example)

progressbar in linux

And the script I'm wanting to add the simple progress bar without any additional installations for the users.

#!/bin/bash

###     Title:          Archive and backup script
###     Date:           16 July 2022
###     Author:         CJG
###     Version:        1.0.0


# The path of the folder you want to back up (change to folder of choice)

backup_files="/home/"

# The backup destination path (change from tmp otherwise it will be deleted upon system reboot)
dest="/tmp"

# Create a variable for the start and end times

# date command will take the current time when the now variable is called
# %H will format output to show the hour when variable is called
# %M will format output to show the minutes when variable is called
# %S will format output to show the seconds when the variable is called

now=$(date +"%H:%M:%S")

# Create the archive filename

currentDate=$(date +"%y-%m-%d")
hostname=$(hostname -s)
archive_file="$hostname-$currentDate.tgz"

# Inform that the process is starting 

echo "Backing up $backup_files to $dest/$archive_file"
echo "The process is starting."

# Print the start time 


echo Script start time: "$now"

# Back up the files using the tar command

tar -czPf $dest/$archive_file $backup_files

# Print the end status message

echo "Backup Is Complete"
now=$(date +"%H:%M:%S")
echo Script end time: "$now" 
Cege
  • 9
  • 1
  • 1
    What makes you think that progress bar is built into Linux, rather than being provided by a library used by `apt`? – chepner Jul 17 '22 at 15:46
  • Can you create a temporary library using apt to inject a progress bar? This doesn't require any escalated privileges of any sort? – Cege Jul 17 '22 at 15:48
  • @chepner It looks like ``` echo 'Dpkg::Progress-Fancy "1";' > /etc/apt/apt.conf.d/99progressbar ``` BUT: This wouldn't work for what I'm trying to do, correct? As it states below. Note that the progress bar only appears when running an apt command, not an apt-get command; e.g., ‘apt update’, ‘apt upgrade’ and ‘apt install’. – Cege Jul 17 '22 at 15:51
  • a simple web search on `site:stackoverflow.com bash progress bar` brings up a slew of SO hits with quite a few answers that require no additional 'installations', eg, [this](https://stackoverflow.com/q/238073), [this](https://stackoverflow.com/q/63055737), [this](https://stackoverflow.com/q/60597755), [this](https://stackoverflow.com/q/23630501) and [this](https://stackoverflow.com/q/41207932) – markp-fuso Jul 17 '22 at 15:57
  • if using 'basic' bash capabilities the general idea is to have the script switch back and forth between a) doing something and b) printing/updating the status bar; this in turn is typically implemented via some sort of loop (eg, `while loop; do do_stuff; update_bar; done`; several of the links I provided (see previous comment) show this; for this particular case you could have a `find` (looking for files to add to archive) feeding a `while` loop, and within the `while` loop you a) add a file (generated by `find`) to the archive and b) update/print the status bar – markp-fuso Jul 17 '22 at 16:00
  • if you need to implement some sort of multi-line status bar you'll likely need to incorporate something like `tput` into the mix (whether or not `tput` is already available will depend on what your sysadmin considered as part of a 'default' installation); see [this](https://stackoverflow.com/a/59554645) for an example using `tput` to manage a multi-line status bar – markp-fuso Jul 17 '22 at 16:04
  • Thank you I appreciate it! I just added the simple echo'd progress bar method that you linked and it looks legit! I know it's not actually accurate to what is being done but it's a perfect start for me. Looking over some of the other materials is very overwhelming and I have no idea what half of it means yet.... but I'll get there one day! Thank you again! – Cege Jul 17 '22 at 16:13

1 Answers1

0

Yes - you can but is not as that straight forward as you think.

You can use ANSI escape codes

here is a really simple one

for index in {1..10}; do sleep 0.1; printf "\e[D#\e[C" ; done; echo
  • \e[D move the cursor to left
  • \e[C move the cursor to right

And the above for is not practical so much, because progress bar is an indicator for unfinished and usually background jobs.

Here is a better version, that prints # while the background jobs has not finished yet

#!/bin/bash

sleep 5 &
sleep_pid=$!;

echo PID: $sleep_pid;

while [ -e /proc/$sleep_pid ]; do
    sleep 0.1; printf "\e[D#\e[C" ;
done
echo

echo PID: $sleep_pid done

help

  • $! Process ID
  • -e FILE True if file exists.

Please note if a task takes long time to be finished, the while loop prints that long time which is not desired in terms of UX. So the speed rate should be calculated to have a proper progress bar.

Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44