Skip to content

Instantly share code, notes, and snippets.

@whiteinge
Created May 31, 2012 01:58
Show Gist options
  • Save whiteinge/2840370 to your computer and use it in GitHub Desktop.
Save whiteinge/2840370 to your computer and use it in GitHub Desktop.
Experimental map shell function using xargs
# Pull the username and password out of the xml file and into local vars
# Run the output through sed to escape & and _ characters for MySQL
# Note: the shell quoting here gets a little rough. The final command is:
# sed -e 's/[%_]/\&/g'
read dbuser dbpass <<<$(xmap \
'xmlstarlet sel -t -v "//controller-config/database/@" '${XML_FILE}' \
| sed -e '\''s/[%_]/\\\\&/g'\' \
username password)
function xmap() {
# Run a shell command once for every argument passed. The first argument
# should be a single-quoted string appropriate to pass to sh -c.
# For example:
# % xmap 'echo @' foo bar baz
# echo foo
# echo bar
# echo baz
cmd=$1
shift
echo -n ${@} | xargs -r -d" " -I@ sh -c "${cmd}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment