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.
While it doesn't display speed, when copying multiple files, the -v option to the cp command will provide you with progress info.
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.
$2 is the second command-line argument passed to the shell script or function. Also, know as Positional parameters.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With