grep fails in shell script when string is not found
Hello
I am having trouble using grep command. I want to search for each line in first file in second file and if they are present, write to file called successfile else to failfile. Below is the code what is happening is, when the first failure occurs (when a line in first file is not present in second file), the script is killed. Can anyone please tell me what I am doing wrong? Code:
while read line |
I think (!) you want:
a) grep $line $pwdir/$file use quotes around $line, else you run into trouble if it read reads more than one string in a line. b) successcount=$successcount+1 use Code:
((successcount=$successcount)) c) echo $line>>successfile put spaces around the redirection: Code:
echo $line >> successfile You can also consider to do it like this: Code:
if grep "$line" $pwdir/$file grep $line $pwdir/$file if [ "$?" == "0" ] grep gives an exit code, either success or failure. "if" uses it. |
Well looking at your code I am guessing the first file has blank lines?
You create a variable temp that is never used. To answer your questions though, use you grep in the if: Code:
if grep -q $line $pwdir/$file |
Thanks for the quick responses. I have incorporated all of your suggestions. The script is working as expected. Thanks much!!
|
I would add that you can use normal increment (like C) in bash as well:
Code:
(( successcount++ )) |
All times are GMT -5. The time now is 11:53 PM. |