Added error handlers

This commit is contained in:
Jeroen De Meerleer 2018-12-01 11:13:30 +01:00
parent a0d0d30989
commit c9b24e4ec2
2 changed files with 23 additions and 1 deletions

View File

@ -32,6 +32,7 @@ Exit Codes:
2 Invalid Argument 2 Invalid Argument
3 Source Unreachable 3 Source Unreachable
4 Destination Unreachable 4 Destination Unreachable
5 Command failed
``` ```

View File

@ -131,6 +131,7 @@ Usage() {
echo " 2 Invalid Argument" echo " 2 Invalid Argument"
echo " 3 Source Unreachable" echo " 3 Source Unreachable"
echo " 4 Destination Unreachable" echo " 4 Destination Unreachable"
echo " 5 Command failed"
echo "" echo ""
} }
@ -243,14 +244,18 @@ CopyFiles() {
if [[ "$dest/$line" = */* ]]; then if [[ "$dest/$line" = */* ]]; then
mkdir -p "$dest/${line%/*}"; mkdir -p "$dest/${line%/*}";
fi; fi;
cp -f $temp/$line $dest/$line 1>/dev/null 2>/dev/null cp -f $temp/$line $dest/$line 1>/dev/null 2>/dev/null
done < "/tmp/music-sync-filelist" done < "/tmp/music-sync-filelist"
VerboseOutput 2 "Copied: $line" VerboseOutput 2 "Copied: $line"
} }
CleanUp() { CleanUp() {
VerboseOutput 1 "Cleaning Up" VerboseOutput 1 "Cleaning Up"
rm "/tmp/music-sync-filelist" if [[ -f /tmp/music-sync-filelist ]]; then
rm "/tmp/music-sync-filelist"
fi
VerboseOutput 1 "Done" VerboseOutput 1 "Done"
} }
@ -260,6 +265,21 @@ ExecTime() {
VerboseOutput 1 "$(($difftimelps / 60)) minutes and $(($difftimelps % 60)) seconds elapsed for Script Execution." VerboseOutput 1 "$(($difftimelps / 60)) minutes and $(($difftimelps % 60)) seconds elapsed for Script Execution."
} }
ErrorHandler() {
VerboseOutput 5 "Error while executing $1"
CleanUp
exit 5
}
ExitHandler() {
VerboseOutput 5 "Aborted"
CleanUp
exit 0
}
trap 'ExitHandler' SIGINT
trap 'ErrorHandler $BASH_COMMAND' ERR
CheckDeps CheckDeps
GetOptions $@ GetOptions $@
if [[ $help == true ]]; then if [[ $help == true ]]; then
@ -272,6 +292,7 @@ fi
CreateFileList $source $dest "" CreateFileList $source $dest ""
if [[ ! -f /tmp/music-sync-filelist ]]; then if [[ ! -f /tmp/music-sync-filelist ]]; then
VerboseOutput 2 "Nothing to do!" VerboseOutput 2 "Nothing to do!"
CleanUp
exit 0 exit 0
fi fi
ConvertFiles ConvertFiles