Last active
August 9, 2019 04:34
-
-
Save sdeering/924d50edd6b5c1bb83f29d31b4f67b2e to your computer and use it in GitHub Desktop.
generate xml sitemap using bash script and jq to fetch remote json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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