Trying to code a shell script for a program testing. i need to check for the existence of particular word in outfile and then proceed further, which i am not able to do. I have wrote the following code. Please tell the corrections.. The control does not pass to the pass condition...

g++ findword.cpp
./a.out < file2.txt > outfile
if read outfile = "enter the word to be matched word found"]
then
echo pass
else
echo fail
fi

you need grep -q (Chant man grep then check the -q option, then experiment a little bit until you understand how it works.) Hint: A zero status is the way Unix utilities signal 'it worked'; so you can use it in a logic test.

Trying to code a shell script for a program testing. i need to check for the existence of particular word in outfile and then proceed further, which i am not able to do. I have wrote the following code. Please tell the corrections.. The control does not pass to the pass condition...

g++ findword.cpp
./a.out < file2.txt > outfile
if read outfile = "enter the word to be matched word found"]
then
echo pass
else
echo fail
fi
printf "%s " "enter word to be matched: "
IFS= read -r word
if grep -q "$word" outfile
then
  echo pass
else
  echo fail
fi

cat filename | grep word | echo "$?"

if the result of above oneliner is 0 it means the word is there in the file . if it's non zero
it means word is not there.(echo "$?" tells us whether the previous command was successful
or not)


let say your file is abc.txt and you want to search the word "john" in it.
cat abc.txt | grep john | echo "$?"

cat filename | grep word | echo "$?"

if the result of above oneliner is 0 it means the word is there in the file . if it's non zero
it means word is not there.(echo "$?" tells us whether the previous command was successful
or not)


let say your file is abc.txt and you want to search the word "john" in it.
cat abc.txt | grep john | echo "$?"

# why this:
cat filename | grep word
# instead of:
grep word filename

@smrati-the code you wrote is just too complex when we can write simpler codes ....using pipe in scripting is required if their are many operations to be performed...
just by writing filename we can work with grep command
not required

cat filename | grep word

just write this way....

grep word filename
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.