2

I have a to find for list of words in a list of files. Hence I have put the list of words in a file, and using for loop trying to read each word in a file and use find command to grep for that word in list of found files.

I am using the mentioned flavor of Linux

bash-3.1$ uname
HP-UX

And below mentioned is my shell script.

  #!/bin/sh
PATH="/NO/20171013"
STRING=`/usr/bin/cat /home/test/STAT44_test.txt`
for LINE in ${STRING}
do
  echo "find ${PATH} -type f -name \"*.txt\" -exec grep -w "${LINE}" {} \; 2>/dev/null | /usr/bin/wc -l"
  LINES=`find ${PATH} -type f -name "*.txt" -exec grep -w "${LINE}" {} \; 2>/dev/null | /usr/bin/wc -l`
  echo "LINES count is ${LINES}"
  if [ ${LINES} -eq 0 ]
  then
        echo "In not found"
#       echo "${LINE} is not found in ${PATH}" >> /home/test/STAT44_not_found.out
  else
        echo "In Found"
#       echo "${LINE} is found in ${PATH}" >> /home/test/STAT44_found.out
  fi
done

The find command find ${PATH} -type f -name "*.txt" -exec grep -w '${LINE}' {} \; 2>/dev/null works perfectly in the command prompt, whereas it is not at giving any output, if used in the shell script as mentioned above.

The output of echo $? is 0 but the find command doesn't produce any output.

The output of the script is

find /NO/20171013 -type f -name "*.txt" -exec grep -w 655044810645 {} \; 2>/dev/null | /usr/bin/wc -l
LINES count is 0
In not found
find /NO/20171013 -type f -name "*.txt" -exec grep -w 734729751028 {} \; 2>/dev/null | /usr/bin/wc -l
LINES count is 0
In not found

Actually the *.txt files under /NO/20171013 has files with pattern 655044810645 and 734729751028 . Hence the Word count should not be 0 and it has to get into else part.

What could be missing?

0

2 Answers 2

6

$PATH is important variable with meaning. You should choose another name.

In this case find executable cannot be found because $PATH doesn't lead to it. You are not notified because of 2>/dev/null.

echo is probably a shell builtin, it needs no $PATH to work. The same for for etc.

3
  • Okay. Now I changed the PATH to PATHS, even then it isn't working. I am getting the below output. /NO/20171013/ 655044810645 734729751028 find /NO/20171013/ -type f -name "*.txt" -exec grep -w '655044810645' {} \; 2>/dev/null | /usr/bin/wc -l 0 LINES 0 find /NO/20171013/ -type f -name "*.txt" -exec grep -w '734729751028' {} \; 2>/dev/null | /usr/bin/wc -l 0 LINES 0 Oct 16, 2017 at 8:52
  • @steeldriver These backticks may be right. With cat they make the content of STAT44.txt be assigned to the variable. Oct 16, 2017 at 10:36
  • @KamilMaciorowski oops you are right - must remember not to post before coffee ;) Oct 16, 2017 at 10:38
2

First, set up a proper $PATH (and see answer by Kamil Maciorowski):

Logg in with suitable user (root?) and execute:

echo PATH=$PATH

Add the output in the start of your script

OR you can add a full path to find command. If you don't know where find are placed, execute:

which find

Output will be like this: /usr/bin/find

And you have a mistake inside loop:

You need to replace single quotes with doublequotes (around of ${LINE}), otherwise the variable will not be interpreted:

echo "find ${PATHS} -type f -name "*.txt" -exec grep -w "${LINE}" {} \; 2>/dev/null | wc -l"
LINES=`find ${PATHS} -type f -name "*.txt" -exec grep -w "${LINE}" {} \; 2>/dev/null | wc -l`

Some notes:

  1. You do not need cat file, try to use loop instead. But use only while, not for! Read Why you don't read lines with "for" and How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
6
  • +1 Because you cover issues my answer doesn't (I decided not to duplicate them). Oct 16, 2017 at 11:09
  • @Egor Vasilyev OKay I understand the usage of for and while now. But find command isn't returning any output. I even changed the single quotes to double quotes, even then the find isn't returning any output. Oct 16, 2017 at 12:01
  • @sabarish jackson, can you update your answer with the latest script code? Oct 16, 2017 at 12:05
  • @EgorVasilyev I have edited my question with output and edits in script. Please look into it. Oct 16, 2017 at 12:19
  • @sabarish jackson, $PATH - is built-in shell variable. You need to use different variable name such as $PATH_TO_FOLDER or something else. See answer by Kamil Maciorowski Oct 16, 2017 at 12:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .