Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdeering/924d50edd6b5c1bb83f29d31b4f67b2e to your computer and use it in GitHub Desktop.
Save sdeering/924d50edd6b5c1bb83f29d31b4f67b2e to your computer and use it in GitHub Desktop.
generate xml sitemap using bash script and jq to fetch remote json
#!/bin/bash
# Bash script to generate a sitemap.xml from external json data
# Author: Sam Deering
# POSIX variable reset in case getopts has been used previously
OPTIND=1
# Initialize our own variables:
URL='http://localhost:4200/items.json'
while getopts "u:" opt; do
case "$opt" in
u) URL=$OPTARG
;;
esac
done
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
# generate sitemap.xml
echo "<?xml version=\"1.0\"?>" > sitemap.xml
echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">" >> sitemap.xml
# homepage
echo "<url>" >> sitemap.xml
echo "<loc>https://example.com/</loc>" >> sitemap.xml
echo "<changefreq>daily</changefreq>" >> sitemap.xml
echo "<priority>1.0</priority>" >> sitemap.xml
echo "</url>" >> sitemap.xml
# collection item pages
ITEMS=$(curl $URL)
echo "$ITEMS" | jq -c '.data[]' | while IFS= read -r line; do
echo "<url>" >> sitemap.xml
PERMALINK=$(echo "$line" | jq -j '.permalink')
echo "<loc>${PERMALINK}</loc>" >> sitemap.xml
echo "<changefreq>weekly</changefreq>" >> sitemap.xml
echo "<priority>0.5</priority>" >> sitemap.xml
echo "</url>" >> sitemap.xml
done
echo "</urlset>" >> sitemap.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment