Skip to content

Instantly share code, notes, and snippets.

@MartyLake
Created May 28, 2017 22:18
Show Gist options
  • Save MartyLake/05817c4784efae26491f2788f9878d0d to your computer and use it in GitHub Desktop.
Save MartyLake/05817c4784efae26491f2788f9878d0d to your computer and use it in GitHub Desktop.
consolidate m3u files to a folder
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
#set -x #debug mode
HELPSTRING="Usage:
$ sh consolidateM3u.sh -p \"./playlist.m3u\" -o \"./outputDir/\""
function printHelp {
echo "$HELPSTRING"
}
#####################################
# Parse the arguments
while getopts "o:p:h" option
do
case $option in
o)
OUTPUTDIR="$OPTARG"
;;
p)
PLAYLISTPATH="$OPTARG"
;;
h)
printHelp
exit 1
;;
:)
#echo "Option $OPTARG requires an argument"
printHelp
;;
\?)
#echo "Unknown option $OPTARG"
printHelp
;;
esac
done
shift $((OPTIND-1)) #remove left arguments
if [ -z "${OUTPUTDIR:-}" ] || [ -z "${PLAYLISTPATH}" ]
then
printHelp
exit 1
fi
if [ ! -f "${PLAYLISTPATH:-}" ]
then
echo "Playlist path '${PLAYLISTPATH:-}' not found."
exit 1
fi
if [ ! -d "${OUTPUTDIR:-}" ]
then
mkdir -p ${OUTPUTDIR}
fi
#####################################
# Copy the files
for file in $(<$PLAYLISTPATH)
do
file=${file//[$'\t\r\n']} && file=${file%%*( )} # trim variable
if [ ! -f "$file" ]
then
echo "NOT FOUND :\"$file\""
else
cp "$file" ${OUTPUTDIR}/
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment