Last active
July 25, 2021 19:56
-
-
Save mtvbrianking/1daa8771b4e25404ef21bf703ea0b940 to your computer and use it in GitHub Desktop.
xmllint indent and file overwrite helpers
This file contains hidden or 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
# https://stackoverflow.com/a/40190976/2732184 | |
# https://unix.stackexchange.com/a/50202/315380 | |
# https://askubuntu.com/a/222371/575565 | |
file="" | |
indent=" " | |
overwrite=0 | |
while [ $# -gt 0 ] | |
do | |
unset OPTIND | |
unset OPTARG | |
while getopts i:o:w options | |
do | |
case $options in | |
i) | |
indent="$OPTARG" | |
# echo "--indent=$indent" | |
;; | |
o) | |
output="$OPTARG" | |
# echo "--output=$output" | |
;; | |
w) | |
overwrite=1 | |
# echo "--overwrite" | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
# file="${file} $1" | |
file="${1:-$file}" | |
shift | |
done | |
# echo "File=$file" | |
if [ -z "$file" ]; then | |
echo "No file specified!" | |
exit 1 | |
fi | |
if [ ! -z "$output" ]; then | |
XMLLINT_INDENT="$indent" xmllint "$file" --format -o "$output" | |
exit 1 | |
fi | |
if [ "$overwrite" -eq 1 ]; then | |
# XMLLINT_INDENT="$indent" xmllint "$file" --format > "$file.tmp" && mv "$file.tmp" "$file" | |
XMLLINT_INDENT="$indent" xmllint "$file" --format --output "$file.tmp" && mv "$file.tmp" "$file" | |
exit 1 | |
fi | |
XMLLINT_INDENT="$indent" xmllint "$file" --format | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lint
.xml
files in a directoryxmltidydir.sh
Usage
Source: https://unix.stackexchange.com/a/50202/315380