Skip to content

Instantly share code, notes, and snippets.

@Ram-Z
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save Ram-Z/f305eddfe4eb6fa3ef98 to your computer and use it in GitHub Desktop.

Select an option

Save Ram-Z/f305eddfe4eb6fa3ef98 to your computer and use it in GitHub Desktop.
#!/bin/bash
# takes a list of find patterns and transform it into arguments to find
find_args() {
echo -n "-name '$(glob "$1")'"; shift
while [[ $# -gt 0 ]]; do
echo -n " -o -name '$(glob "$1")'"; shift
done
}
# checks whether $1 contains a globbing pattern
# returns: '$1' or '*$1*'
glob() {
if [[ $1 =~ [[]*?] ]]; then
echo -n "$1"
else
echo -n "*$1*"
fi
}
dir=$(mktemp -d)
touch "$dir"/{foo1,foo2,foo3,bar1,bar2}
declare -a args=('foo1' 'foo2' 'bar*')
echo "without eval"
find "$dir" $(find_args "${args[@]}")
echo "with eval"
eval find "$dir" $(find_args "${args[@]}")
rm -r "$dir"
#!/bin/bash
# takes a list of find patterns and transform it into arguments to find
find_args() {
declare -a fargs=("-name" "$(glob "$1")"); shift
while [[ $# -gt 0 ]]; do
fargs+=("-o" "-name" "$(glob "$1")"); shift
done
echo "${fargs[@]}"
}
# checks whether $1 contains a globbing pattern
# returns: '$1' or '*$1*'
glob() {
if [[ $1 =~ [][*?] ]]; then
echo -n "$1"
else
echo -n "*$1*"
fi
}
dir=$(mktemp -d)
touch "$dir"/{foo1,foo2,foo3,bar1,bar2}
declare -a args=('foo1' 'foo2' 'bar*')
echo "without eval"
find "$dir" $(find_args "${args[@]}")
echo "with eval"
ssh host "find \"$dir\" $(printf "'%s' " $(find_args "${args[@]}"))"
rm -r "$dir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment