Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Bash to display a progress indicator [duplicate]

Using a bash only script, how can you provide a bash progress indicator?

For example, when I run a command from bash - while that command is executing - let the user know that something is still happening.

like image 578
Pez Cuckow Avatar asked Sep 19 '12 15:09

Pez Cuckow


People also ask

How can I check copy progress in Linux?

While it doesn't display speed, when copying multiple files, the -v option to the cp command will provide you with progress info.

How do I copy progress bar in Linux?

If you want to copy multiple files or directories, then using the “tar” command in the terminal is a better option. Specify the source and destination folder in the “tar” command with the option “-C” in combination with “pv” to view the speed and progress of the process.

What does $2 do in bash?

$2 is the second command-line argument passed to the shell script or function. Also, know as Positional parameters.


1 Answers

In this example using SCP, I'm demonstrating how to grab the process id (pid) and then do something while that process is running.

This displays a simple spinnng icon.

/usr/bin/scp [email protected]:file somewhere 2>/dev/null & pid=$! # Process Id of the previous running command  spin[0]="-" spin[1]="\\" spin[2]="|" spin[3]="/"  echo -n "[copying] ${spin[0]}" while [ kill -0 $pid ] do   for i in "${spin[@]}"   do         echo -ne "\b$i"         sleep 0.1   done done 

William Pursell's solution

/usr/bin/scp [email protected]:file somewhere 2>/dev/null & pid=$! # Process Id of the previous running command  spin='-\|/'  i=0 while kill -0 $pid 2>/dev/null do   i=$(( (i+1) %4 ))   printf "\r${spin:$i:1}"   sleep .1 done 
like image 118
Pez Cuckow Avatar answered Sep 24 '22 00:09

Pez Cuckow