bash - How to pass a filename with embedded wildcards to find without limiting to files found locally? -


this question has answer here:

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 , failglob shell option set, function not executed @ all. have not seen because of default shell options.

  • else if glob fails , nullglob shell option set, result of glob nothing @ all, , no argument passed function. have not experienced either.

  • else if glob fails , failglob shell option unset, result of glob whatever glob expression was, , used argument value. then, glob expression passed find, , find want because implements globbing internally. lucky works, works.

  • the glob succeeds , results in list of files used list of arguments function. call find using $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

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -