Check for substring using unix make

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 .)

Upvotes: 1

Views: 1066

Answers (2)

cyon
cyon

Reputation: 9538

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.

Upvotes: 1

darkgrin
darkgrin

Reputation: 580

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.

Upvotes: 2

Related Questions