1

Coding in UNIX make, I have written:

cat $ sString | grep sSubstring > sResult

So I want to check if sString contains sSubString and, if it does, put the result in sResult.

Background: I have put the contents of a file in sString - if sSubString is present in the file, then sResult contains each line of the file containing sSubString.

This works fine when sSubString is in sString. When it is not, I get Error Code 1.

How can I handle this correctly? The complete code is:

cat $ sString | grep sSubstring > sResult
if [ -s sResult -gt 0 ];then \
(echo "substring present" ) \
else (echo "substring not present" ) ;fi

(With the error code, I never get to the else .)

2 Answers2

2

To check if a specific substring is present in a string, you can use the 'findstring' function. More info here: http://www.gnu.org/software/make/manual/html_node/Text-Functions.html.

darkgrin
  • 570
  • 3
  • 8
1

Sounds like you might need this

cat $ sString | { grep sSubstring || true; } > sResult

See this solution. The problem is that when grep doesn't find anything it returns a non-zero exit code. make will then think it is an error. So you just need to make sure that the command always returns a zero exit code by or-ing the exit code of grep with the exit code of true. The exit code of true is always 0.

Community
  • 1
  • 1
cyon
  • 9,340
  • 4
  • 22
  • 26