0

I'm trying to use find with globs in a Bash shell script. I've tried to both sourcing it and running it, but the command fails every time.

#!/usr/bin/env bash
# glob-test.sh - a script to test globs in find
cd /to/a/windows/share-point
find ./**/*Some/*Place/**/ -iname "*.pdf"

# How about using foreign characters?
find ./**/*Söme/*Pläce/**/ -iname "*.pdf"
  • From bash command line it works
  • I have tried different way of quoting it.
  • I have tried sourcing and executing the script.
  • I have tried turning on/off the globstar shell option, both inside and outside the script, but still no go. (shopt -s/u globstar)

What is the problem and how to fix it?

not2qubit
  • 1,578
  • 1
  • 20
  • 21
  • 3
    Please can you explain (in your question) what you mean by "_fails every time_". Error? Nothing appears? (What should appear?) Unexpected results appear? A test scenario you can use to explain this would help too – roaima Apr 29 '21 at 17:42
  • 1
    Is it actually a bash script? How are you running it? What's the shebang? – muru Apr 29 '21 at 17:43
  • LIke I said, I tried running as either. Shebang: `#!/usr/bin/env bash` with `./script.sh` or using `. script.sh` for sourcing. Fails meaning, it says it can's find any files, while everything is found when just copy pasting this into shell. – not2qubit Apr 29 '21 at 20:46
  • Please put that into your question, so that it remains the definitive place for your issue and all its detail – roaima Apr 29 '21 at 20:58
  • I just tested at home, and it works here. The issue was at work. I will test there tomorrow. It's possible its an issue when searching on windows shares... – not2qubit Apr 29 '21 at 21:12
  • Does `echo ./**/*Some/*Place/**/` output the intended list? – U. Windl Apr 29 '21 at 21:13
  • The problem had mainly to do with foreign character sets in the search string, not shown above. See my answer. – not2qubit Apr 30 '21 at 19:22

1 Answers1

0

It seem that the issue was 2-fold,

  • When doing this on a windows share-point, and sourcing it, find returns very slowly, the first time. But much faster the second time. Somehow there must be some kind of buffering/cache'ing going on. So the user thinks that the command failed or blocked. This is not the case when executing. As sourcing is run in the current shell, globstar probably need to be enabled. Running it as a script seem to not have this issue and also seem to be independent of the globstar setting.

  • The result of above, is that somehow the character set also confused find when running as a script vs. sourced. Running sourced would use the locally set character set of the terminal which is correct, while scripted, would use that of the default/OS, AFAICT. (I work in 3 different languages.)

What I did not know or tell in OP, is that I was using another language characters (containing umlauts) in the /*Some/*Place/ part. I didn't think it would matter, but it caused the command to fail. As it secretly was searching for the 2-byte UTF-8 characters, while correctly displaying only one.

I'm not completely sure what is going on under the bash hood here, but fiddling with the character sets, and patienly wait for the results, I got it to work as expected. Feel free to post a better or more clear answer.

not2qubit
  • 1,578
  • 1
  • 20
  • 21