1

This is the content of my text file named fnames.txt:

"SAMPLE_NIKE_856_20170703*"
"SAMPLE_ADIDAS_856_20170702*"
"SAMPLE_ANTA_856_20170630*"
"SAMPLE_JORDAN_856_20170627*"
"SAMPLE_CONVERSE_856_20170229*"

This is my script named fn.sh:

#!/bin/sh
#
#
while read LINE
do
    find -name "$LINE"
done < fnames.txt

It returns nothing.

What I want to happen is that in each line the script will execute the find command and the output will be stored in another text file called files.txt e.g:

LINE 1:

find -name "SAMPLE_NIKE_856_20170703*"

then returns the filename that is looking for

./SAMPLE_NIKE_856_20170703_80_20_304_234_897.dat

LINE 2:

find -name "SAMPLE_ADIDAS_856_20170702*

then returns the filename that is looking for

./SAMPLE_ADIDAS_856_20170702_56_98_123_245_609.dat

The script will continue until all lines has been executed by find command.

  • First your script should return the same results to the standard output as the separate calls of find, if you run your script in the right directory. Secondly, to store the results in a text file you can add echo "$Line" >> out.txt to your loop. – Hölderlin Jul 5 '17 at 3:06
  • I can not understand on what you said "script should return the same results to the standard output as the separate calls of find". Can you provide and example? – WashichawbachaW Jul 5 '17 at 4:06
  • What's the shell that you're using ? What's your OS ? – Sergiy Kolodyazhnyy Jul 5 '17 at 5:39
  • It's BASH #!/bin/sh. I'm using Linux through PuTTY cause my pc OS is Windows. – WashichawbachaW Jul 5 '17 at 7:25
3

The reason is the order the shell processes the various expansions and quotings. If you directly type

find . -name "foo*"

The shell will remove the quotes and pass foo* as parameter to find.

Without the quotes, the shell would perform the globbing for the * first, potentially resulting in multiple matches that would confuse find. So it's wise to use the quotes.

But if you read the string from a file, the variable will contain the quotes. Now, when you do

> echo $LINE
"foo*"
> echo "foo*"
foo*
> find . -name $LINE

you see that the quotes of $LINE will not get removed opposed to the ones for echo "foo*". This is because the shell first handles the quotes of echo $LINE (there aren't any) and then performs the variable substitution, so the line will read echo "foo*", but now the quotes remain.

So your find will look for files containing the quotes. The solution is to leave away the quotes in the file. No quoting is needed there, the complete line will go to the variable.

  • Yeah I understand that part. I tried it out also but still got no result. Can you provide an example script? – WashichawbachaW Jul 5 '17 at 7:23
  • With exactly your script I get results by simply leaving away the double quotes in the file. – Philippos Jul 5 '17 at 7:26
  • Now I get this error: $ /path/source/fn.sh: line 6: fnames.txt: No such file or directory I'm running the script through different directory. That's why it can't find fnames.txt. I tried writing the full path in the script after done, but still is says that error. done < /path/source/fn.sh – WashichawbachaW Jul 5 '17 at 7:47
  • You mean done < /path/to/your/fnames.txt, I suppose? I it says this file can't be found, there is a good chance it's not there! Also don't forget to adjust the path for find with the directoy to search for. – Philippos Jul 5 '17 at 7:53
  • My script is now up and running! Thanks :D – WashichawbachaW Jul 5 '17 at 7:54
1

Modify the IFS to read as :

while IFS=\" read -r x LINE x; do
   find . -name "$LINE"
done < fnames.txt

This will ** essentially strip away the quotes ** and the $LINE will be what you need the find to work properly. What it does is split the line read in on quotes and so they thence won't show up in $LINE.

0

I finally found the answer. I just need to call the full file path of fnames.txt to /path/to/my/fnames.txt because I'm executing the script in another directory.

#!/bin/sh
#
#
while read -r LINE
do
    find -name "$LINE"
done < /path/to/my/fnames.txt

Output:

./SAMPLE_NIKE_856_20170703_80_20_304_234_897.dat
./SAMPLE_ADIDAS_856_20170702_56_98_123_245_609.dat
  • "...I just need to call the full file path of fnames.txt..." that is exactly what I'm trying to tell you in my first point in the comment above with respect to your question. That certainly you run your script in the wrong directory! – Hölderlin Jul 5 '17 at 9:25
  • Sorry didn't understand that part, that's why I ask. Thanks for the help by the way. – WashichawbachaW Jul 6 '17 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.