0

I have simple bash script find.sh for finding the files

==>cat find.sh

echo $1

find -name $1

but it is not taking the correct arguments sometimes, instead it takes the fixed argument

Eg

find.sh 'ECSv2_P_TCP_FUNC_060*'

ECSv2_P_TCP_FUNC_060 ECSv2_P_TCP_FUNC_060.backup

Here though i have passed 'ECSv2_P_TCP_FUNC_060*' it has taken ECSv2_P_TCP_FUNC_060 ECSv2_P_TCP_FUNC_060.backup these as arguments.

Why does this happen? And how to avoid this?

2 Answers 2

5

You need to protect the * character from shell expansion inside the script as well:

echo "$1"
find . -name "$1"

(Edited to include the current directory as argument for find.)

Sign up to request clarification or add additional context in comments.

Comments

1

Your script is indeed taking the argument, but the script is expanding the * before passing it to echo and find is reading the argument and interpreting the *. (Actually, find is probably bombing because the first arguemnt should be a directory. eg, 'find . -name $1')

4 Comments

At least in GNU find, the manpage states: "If no paths are given, the current directory is used."
@Christoph Other finds take it as a syntax error. (BSD find certainly does!)
Thanks a lot!! combining answer 1 and 2 solved my problem. I was missing quotes as well as . required for specifying directoy.
@Christopher: It's different if the argument is a glob. Using GNU find with an existing file named "foo.txt" in the current directory and in a subdirectory below that one with the same name and one called "foo.txt.bak", plus a few other files at both levels, compare each of find foo.*, find "foo.*", find . "foo.*", find -name "foo.*", find . -name "foo.*"` and find . -name foo.*.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.