Skip to content

Instantly share code, notes, and snippets.

@michalblaha
Created March 18, 2024 21:49
Show Gist options
  • Save michalblaha/db6fc8eb27f3602c43d718362206b1a8 to your computer and use it in GitHub Desktop.
Save michalblaha/db6fc8eb27f3602c43d718362206b1a8 to your computer and use it in GitHub Desktop.
Merge GPS multile tracks into one with gpsbabel
#!/bin/zsh
# Initialize variables
files=""
directory="."
outputfn=""
# Function to show usage
usage() {
echo "Usage: $0 -i <file_mask> [-o <filename>] [-d <directory>]"
exit 1
}
# Parse command line options
while getopts ":i:d:" opt; do
case $opt in
i)
file_mask="$OPTARG"
;;
d)
directory="$OPTARG"
;;
o)
outputfn="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
;;
esac
done
# Check if mandatory parameters are provided
if [ -z "$file_mask" ]; then
echo "Option -i is required."
usage
fi
#fill outputfn from filemask
if [ -z "$outputfn" ]; then
outputfn="${file_mask[1,10]}.gpx"
fi
# Check if directory exists
if [ ! -d "$directory" ]; then
echo "Directory '$directory' does not exist."
usage
fi
# Find files matching the mask in the directory and append them to the files variable
while IFS= read -r -d '' file; do
echo $file
files+="-f $file"$' '
done < <(find "$directory" -type f -name "$file_mask" -print0)
# Display found files
echo "Found files:"
echo "$files"
# Now, you can use the $files variable for further processing
startscr="/Applications/GPSBabelFE.app/Contents/MacOS/gpsbabel -w -r -t -i gpx $files -x sort,time -x track,pack,title=$outputfn -o gpx -F $outputfn"
echo "Saving into $outputfn"
eval "$startscr"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment