bash - How to pass a filename with embedded wildcards to find without limiting to files found locally? -
this question has answer here:
- stop shell wildcard character expansion? 5 answers
i've got script uses find files match user input. basically, user enters test* , it's supposed find matching files.
the problem i'm having if there file in current directory matches filename pattern user entered, looks file. is, if user enters fred* , there file in current directory called frederica, script ever finds files named frederica. here's example of problem:
>ls test1 test2 test3 test4 test5 words >tmp() > { > my_file="$1" > find . -iname "$my_file" > } >tmp test* ./test1 > if enter tmp test*, expect return 5 files test1 through test5. instead returns first one. further, if searched on, say, /, still ever return files named test1 every directory.
the actual script more complex, of course, , i've developed workaround involving "wildcard option" (e.g., -w 1 means leading , trailing asterisks, etc.) i'd able let user enter filename wildcard.
thanks in advance
when call function way do, shell interpreting call tries find matching files (this done before entering function), , different things can happen :
if glob fails ,
failglobshell option set, function not executed @ all. have not seen because of default shell options.else if glob fails ,
nullglobshell option set, result of glob nothing @ all, , no argument passed function. have not experienced either.else if glob fails ,
failglobshell option unset, result of glob whatever glob expression was, , used argument value. then, glob expression passedfind, ,findwant because implements globbing internally. lucky works, works.the glob succeeds , results in list of files used list of arguments function. call
findusing$1, first of these arguments taken account, resulting in result not want.
double quoting or single quoting disable globbing. if using script, can therefore do:
tmp "test*" if want use command command line, option entirely disable globbing, set -f, might have drawbacks.
Comments
Post a Comment