The issue is that instead of grepping for
name in the non-commented data in
file.tx, you run
grep against the original file
grep -v '^#' file.tx | grep -P 'name' file.tx
will not read from pipe
grep -v '^#' file.tx | grep -q 'name'
i.e., don't make the second
grep in the pipeline read from the original file. Also, since you are only interested in the exit status of
grep (for use in the
if statement), use it with
Taking your second question into account, it seem, if I understand correctly, that you want to run
cat file.tx only if the string
name is present outside of comments. In this case you have your logic backwards.
if grep -v '^#' file.ex | grep -q 'name'; then
This will be true if the string
name occurs in a non-commented line. If you want to
cat the file, then you would need to do it here:
if grep -v '^#' file.ex | grep -q 'name'; then \
cat file.tx; \
echo 'No name'; \
I'm uncertain about the
exit 0 as that has implications for the rest of the Makefile target. If you want the target to fail if
name is not present, then do
exit 1 after the
echo, otherwise don't exit at all (unless you have further shell code that you don't want to execute in the same Makefile target).
I'm also noticing your
cd ../... Unless you have a
file.tx in the current directory and in the directory
cat would fail for you. You have not mentioned why you step two directory levels up, so I can't say more about this.
Also note that the
cd will be "undone" as soon as the shell snippet is completed as
make runs each individual line of a target in a separate shell instance. This also means that the various
set instructions that you have are run in separate shells.