Skip to content

Instantly share code, notes, and snippets.

@mherwig
Last active October 13, 2015 05:37
Show Gist options
  • Save mherwig/4147262 to your computer and use it in GitHub Desktop.
Save mherwig/4147262 to your computer and use it in GitHub Desktop.
Assistent for copying easily content from a selected directory to several selected USB-devices in few steps
#!/bin/bash
# Author: Mike Herwig
# Version: v12.8-24
# Beschreibung: Assistent for copying easily content from a selected directory to several selected USB-devices in few steps.
#
# Edited by Kai Jansen
# Added unmountFinishedSticks and getSudo Function
#
# Translation (for github:gist) by Mike Herwig
function getSudo
{
AUTHED=$(zenity --password --title "I need more rights!" --text "This script needs superuser rights" | sudo -S echo "authentication successfull" 2>/dev/null)
if [ -z "$AUTHED" ]; then
zenity --info --text "Authentication failed"
exit 1
fi
}
function getMounts
{
LIST=()
DEV_LIST=($(ls /dev | grep sd[b-z]1))
MOUNTS_LIST=($(awk '$1 ~ /\/dev\/sd[b-z]+/ { print $2 }' < /proc/mounts))
INDEX=0
for i in ${DEV_LIST[@]}; do
LIST=(${LIST[@]} 'TRUE')
LIST=(${LIST[@]} ${DEV_LIST[$INDEX]})
LIST=(${LIST[@]} ${MOUNTS_LIST[$INDEX]})
INDEX=$((INDEX + 1))
done
}
function showMountSelection
{
SELECTION=($(zenity --list --checklist --width=500 --height=250 --text \
"Where to copy the files to?\n(usb sticks commonly get mounted at /media or /run/media)" --title \
"Choose disk" --column="" --column Device --column Mount-Point \
--separator=" " --print-column=3 ${LIST[@]}))
if [ -z "$SELECTION" ]; then
echo "terminated by user"
zenity --info --text "terminated by user"
exit 1
fi
}
function showSourceSelection
{
SOURCE_DIR=`zenity --file-selection --filename=/home/share/ --text \
"Select the directory with the content you whish to copy" --title="Directory selection" \
--directory --separator=" "`
if [ -z "$SOURCE_DIR" ]; then
echo "terminated by user"
zenity --info --text "terminated by user"
exit 1
fi
}
function startCopy
{
exec 3> >(zenity --progress --title="Copying" --percentage=0 \
--auto-close --width=400)
SELECTION_COUNT=${#SELECTION[@]}
INDEX=0
for i in ${SELECTION[@]}; do
echo "Copy files to '${SELECTION[$INDEX]}' ($((INDEX + 1))/$SELECTION_COUNT)" >&3;
cp -R $SOURCE_DIR/* ${SELECTION[$INDEX]}
echo "Done copying files to '${SELECTION[$INDEX]}'" >&3;
INDEX=$((INDEX + 1))
echo $((INDEX * 100 / $SELECTION_COUNT)) >&3;
done
exec 3>&-
}
function verifyFiles
{
IFS=$'\n'
MEDIA_FILES_LIST=($(find ${SELECTION[0]} | sed "s#${SELECTION[0]}/*##g" | sort))
SOURCE_FILES_LIST=($(find $SOURCE_DIR | sed "s#$SOURCE_DIR/*##g" | sort))
for x in ${SOURCE_FILES_LIST[@]}; do
MATCH=0
for y in ${MEDIA_FILES_LIST[@]}; do
if [ "$x" == "$y" ]; then
MATCH=1
continue 2
fi
done
break
done
unset IFS
}
function unmountFinishedSticks
{
exec 3> >(zenity --progress --title="Unmount devices..." --percentage=0 \
--auto-close --width=400)
if [ "$MATCH" == "1" ]; then
UNMOUNT_COUNT=${#SELECTION[@]}
INDEX=0
for MOUNT in ${SELECTION[@]}; do
echo "Unmounting: ${MOUNT}"
umount ${MOUNT}
sleep 0.2
if [ -e ${MOUNT} ]; then
zenity --info --text "${MOUNT} couldn't get unmounted"
fi
INDEX=$((INDEX + 1))
echo $((INDEX * 100 / ${UNMOUNT_COUNT})) >&3;
done
fi
exec 3>&-
}
function showFinishDialog
{
if [ "$MATCH" == "1" ]; then
REPEAT_MAIN_LOOP=0
zenity --question --text "That was great. Do it again?"
if [ "$?" != "0" ];then
zenity --info --text "As you wish my lord, see you next time!"
exit 0
fi
else
REPEAT_MAIN_LOOP=1;
zenity --info --text "Warning:\nSome files haven't been successfully copied to the selected devices,\ntry it again or contact the developers"
fi
}
function mainLoop
{
while [ "$REPEAT_MAIN_LOOP" == "0" ]; do
getMounts
showMountSelection
startCopy
verifyFiles
unmountFinishedSticks
showFinishDialog
done
}
getSudo
showSourceSelection
REPEAT_MAIN_LOOP=0;
mainLoop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment