Skip to content

Instantly share code, notes, and snippets.

@jeffguorg
Last active August 6, 2021 03:05
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 jeffguorg/51a129a04bc9e11fb05731e94e8f2aa7 to your computer and use it in GitHub Desktop.
Save jeffguorg/51a129a04bc9e11fb05731e94e8f2aa7 to your computer and use it in GitHub Desktop.
inplace function in zsh
#!/usr/bin/env zsh
inplace() {
COMMAND="$1"
FILEPATH="$2"
set -x
OUTPUT="$(bash -c "${COMMAND} '${FILEPATH}'")"
RET=$?
if [ $RET -eq 0 ]; then
echo -n "$OUTPUT" >! "${FILEPATH}"
else
echo -n "$OUTPUT"
return $RET
fi
}
@jeffguorg
Copy link
Author

jeffguorg commented Aug 5, 2021

if i'm to prettify all the xml documents in current directory, imma run this command:

for filepath in "${(@f)$(find . -not -regex '\./\.git/.*' -type f)}"; do inplace 'xmllint --pretty 1' "$filepath"; done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment