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

1
  • FWIW, I've tried other other string contains solutions here, but those solutions do not work on this make file. Commented Oct 23, 2012 at 15:23

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Amazing! That was quick, and it works perfectly. Thanks so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.