Skip to content

Instantly share code, notes, and snippets.

@RWJMurphy
Last active April 7, 2016 19: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 RWJMurphy/58600ba58923a9d3c362a2d76001aa18 to your computer and use it in GitHub Desktop.
Save RWJMurphy/58600ba58923a9d3c362a2d76001aa18 to your computer and use it in GitHub Desktop.
#!/bin/bash
function silent() {
"$@" >/dev/null 2>&1
}
function command_exists() {
silent hash "$@"
}
function not() {
! "$@"
}
function map() {
declare -a basecmd cmd
declare -i substitution=0
basecmd=("$@")
# TODO: make xargs or parallel work with funcs like `silent` :|
# if command_exists parallel; then
# parallel --keep-order "${basecmd[@]}"
# elif command_exists xargs; then
# # TODO: detect FreeBSD parallel option
# xargs -I{} "${basecmd[@]}"
# else
if [[ "${cmd[*]}" == "*{}*" ]]; then
substitution=1
fi
while read line; do
if [ $substitution = 1 ]; then
cmd=("${basecmd[@]//{}/$line}")
else
cmd=("${basecmd[@]}" $line)
fi
"${cmd[@]}" && echo "$line"
done
# fi
}
function filter() {
map silent "$@"
}
@RWJMurphy
Copy link
Author

$ echo -e "google.com\nyahoo.com\nbaddomain------------llkjlkj.com" | filter host
google.com
yahoo.com
$ echo -e "google.com\nyahoo.com\nbaddomain------------llkjlkj.com" | filter not host
baddomain------------llkjlkj.com

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