Last active
August 29, 2015 14:19
-
-
Save Ram-Z/f305eddfe4eb6fa3ef98 to your computer and use it in GitHub Desktop.
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
| #!/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" |
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
| #!/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