Skip to content

Instantly share code, notes, and snippets.

@markhalliwell
Last active January 22, 2016 13:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markhalliwell/ae6e229c33453f2547a8 to your computer and use it in GitHub Desktop.
Save markhalliwell/ae6e229c33453f2547a8 to your computer and use it in GitHub Desktop.
This only really works if rsyncing entire folders. Single files do work, however it will just appear to stand still (no progress). Also, change the path to cocoaDialog accordingly.
#!/bin/bash
# cocoaDialog Rsync Progress
#
# Rsync files using the cocoaDialog progress bar. Example usage:
# $ rsync -avr --progress ~/source ~/destination | ./rsync-progress.sh
#
# Copyright (C) 2014 Mark Carver
#
# Adapted from Kevin Hendricks example code
# http://mstratman.github.io/cocoadialog/examples/progressbar.sh.txt
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Create a named pipe.
rm -f /tmp/hpipe
mkfifo /tmp/hpipe
# Create a background job which takes its input from the named pipe.
~/Applications/cocoaDialog.app/Contents/MacOS/cocoaDialog progressbar --float --resize --minimize --title "Syncing Files" --text "Syncing..." --icon sync < /tmp/hpipe &
# Associate file descriptor 3 with that pipe.
exec 3<> /tmp/hpipe;
# Initialize the current percentage.
currentPercent=0;
# Read stdin, line by line.
while read line; do
# Output rsync output as normal (for logging purposes).
echo ${line};
# Parse the rsync output so it can be sent to the progress bar.
line=$(echo ${line} | awk '{
if (index($0, "to-chk=") > 0) {
split($0, pieces, "to-chk=")
split(pieces[2], term, ")");
split(term[1], division, "/");
print (1-(division[1]/division[2]))*100
}
else if (index($0, "ir-chk=") > 0) {
split($0, pieces, "ir-chk=")
split(pieces[2], term, ")");
split(term[1], division, "/");
print (1-(division[1]/division[2]))*100
}
else {
print "0 "$0;
}
fflush();
}');
# Parse the percentage number.
percent=$(echo ${line} | sed -E 's/^([0-9\.]) [^\/]*\/(.*)/\1/');
# Only increase the percentage if not "0". This allows the filenames to be updated.
if [[ "${percent}" != "0" ]]; then
currentPercent="$percent";
fi
# Parse the filename being synced.
filename=$(echo ${line} | sed -E 's/^([0-9\.]) [^\/]*\/(.*)/\2/');
# When there is no filename the percentage is parsed instead, reset it.
if [[ "${filename}" == "${currentPercent}" ]]; then
filename='';
fi
# Output the current percentage and filename to the progress bar file descriptor 3.
echo "${currentPercent} ${filename}" >&3;
done < /dev/stdin;
# Turn off the progress bar file descriptor 3.
exec 3>&-;
# Wait for all background jobs to exit.
wait;
# Remove the named pipe.
rm -f /tmp/hpipe
# Exit successfully.
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment