TARGET='cat $TMP |grep $TXT | wc -l'
This assigns the literal string 'cat $TMP |grep $TXT | wc -l'
to the variable $TARGET
.
It looks like what you want is the output of the command, which requires backticks:
TARGET=`cat $TMP |grep $TXT | wc -l`
or, if you have a reasonably modern shell, the $(...)
syntax:
TARGET=$(cat $TMP |grep $TXT | wc -l)
Furthermore, that command can be simplified considerably, from the above to this:
TARGET=$(grep $TXT $TMP | wc -l)
to this:
TARGET=$(grep -c $TXT $TMP)
Finally, the $TARGET
variable can be eliminated altogether if you change the if
statement from this:
if [ "$TARGET" -eq "1" ];
to this:
if [ $(grep -c "$TXT" "$TMP") = 1 ];
Or you can use [[
... ]]
rather than [
... ]
(it's preferred for bash).
Or, if you only care whether the pattern occurs at all (rather than requiring it to appear exactly once):
if grep -q "$TXT" "$TMP";
For that matter, you can eliminate the $TMP
file as well; I'll leave that as an exercise. 8-)}
Consult the documentation for grep
to see what the options do. (The -c
option tells it to print the number of matches, -q
prints nothing but still sets the status to indicate whether the pattern was found).
Note that I also added quotation marks around the variable references, which is good practice if there's any possibility that their values might contain any special characters.