BASH: can grep on command line, but not in script
Solution 1
Are you sure that $TITLE
does not have leading or trailing whitespace which is not in the file? Your fix with the string would strip out whitespace before execution, so it would not see it.
For example, with a file containing 'Line one':
/home/user1> TITLE=' one '
/home/user1> grep "$TITLE" text.txt
/home/user1> cat text.txt | grep $TITLE
Line one
Try echo "<$TITLE>"
, or echo "$TITLE"|od -xc
which sould enable you to spot errant chars.
Solution 2
This command
echo "cat PAGE.$TEMP.2 | grep $TITLE"
echoes a string that starts with 'cat'. It does not run a command. You would want
echo "$( cat PAGE.$TEMP.2 | grep $TITLE )"
although that is identical in functionality to the simpler
cat PAGE.$TEMP.2 | grep $TITLE
And as pointed out by others, there is no need to pipe a single file using cat
; grep
can read from files just fine:
grep "$TITLE" "PAGE.$TEMP.2"
(Your default behavior should be to quote parameter expansions, unless you can show it is incorrect to do so.)
Related videos on Youtube

Crazy_Bash
Updated on September 18, 2022Comments
-
Crazy_Bash less than a minute
Did this million times already, but this time it's not working I try to grep "$TITLE" from a file. on command line it's working, "$TITLE" variable is not empty, but when i run the script it finds nothing
*title contains more than one word
echo "$TITLE" cat PAGE.$TEMP.2 | grep "$TITLE"
what i've tried:
echo "cat PAGE.$TEMP.2 | grep $TITLE"
to see if title is not empty and file name is actually there
-
Šimon Tóth about 10 yearsHave you actually tried to
grep
the file with the value? Maybe the title really isn't in the file. -
cdarke about 10 yearsBut the first time you had quotes around the
"$TITLE"
, the second time you don't.
-
-
Crazy_Bash about 10 yearsWORKED! thanks man! od -xc helped me, great tool. i added tr -d '\r\n\' and that fixed the problem.