LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   REXX code executes grep, generates spurious error message (https://www.linuxquestions.org/questions/linux-newbie-8/rexx-code-executes-grep-generates-spurious-error-message-806946/)

danielbmartin 05-10-2010 08:04 AM

REXX code executes grep, generates spurious error message
 
I'm porting my Reginald/Windows REXX programs to Regina/Linux. This requires minor recoding. However, I now realise that large clumps of REXX code may be replaced with small clumps of Linux commands. The result is fewer lines of code and, in some cases, *much* shorter execution time.

My recoded programs exhibit a vexing cosmetic problem. To illustrate with a contrived example which uses a famous Edgar Allen Poe poem:

"grep 'Quoth' < " raven

This works and prints the five lines which contain the word Quoth.


"grep 'Spoke' <" raven

The poem does not contain the word Spoke, so nothing should be printed. However, executing this code from within my Regina REXX program produces this:
11 *-* "grep 'Spoke' <" raven
+++ RC=1 +++

I don't consider this an error. grep searched for a specific character string and printed all that were found (none).

How may I suppress this spurious error message?

Daniel B. Martin

Tinkster 05-11-2010 01:07 PM

You can't really stop grep from returning a return code.

Now it's been about 11 years that I last touched REXX,
plus I don't know what your code is meant to achieve,
so can't tell you HOW it's done. What you need to do
is to capture the output of the grep in a variable and
return the result explicitly if there was one.



Cheers,
Tink

makyo 05-11-2010 04:45 PM

Hi.

You could write your own script that runs grep and always returns an exit status of zero, then call your script in place of grep.

If you wanted a more complete version, you could have your script return zero if the grep exit code were zero or one, and the actual status if other than either of those ... cheers, makyo

makyo 05-12-2010 04:17 AM

Hi.

Here's an example of a script that returns 0 for grep exit status 0,1, "my-grep":
Code:

#!/usr/bin/env bash

# @(#) my-grep        Make grep exit status 1 into 0.

grep "$@"
status=$?

if [ "$status" -gt 1 ]
then
  exit $status
else
  exit 0
fi

So, for a data file "data1":
Code:

foo
bar
baz

the rexx script "s1":
Code:

#!/usr/bin/rexx -

/*

# @(#) s1        Demonstrate results of wrapper script.

*/

say
say ' Hello, world from rexx.'

say
say " Version of rexx:"
"rexx -version"

say
say " Looking for bar."
"grep bar < data1"

say
say " Looking for qux, expect message."
"grep qux < data1"

say
say " Looking for qux, made into 0 by wrapper script, expect no output."
"./my-grep qux < data1"

say
say " Real error results in a message."
"./my-grep a b c"

say
say " At the end."

when run, produces:
Code:

% ./s1

 Hello, world from rexx.

 Version of rexx:
REXX-Regina_3.3 5.00 25 Apr 2004

 Looking for bar.
bar

 Looking for qux, expect message.
    22 *-* "grep qux < data1"
      +++ RC=1 +++

 Looking for qux, made into 0 by wrapper script, expect no output.

 Real error results in a message.
grep: b: No such file or directory
grep: c: No such file or directory
    30 *-* "./my-grep a b c"
      +++ RC=2 +++

 At the end.

The script needs to be a directory in your PATH collection, or run from the current directory as I did in this script with "./my-grep ...".

Best wishes ... cheers, makyo

danielbmartin 06-07-2010 08:59 AM

Thank you, Mayko, for your interest. Real-life demands distracted me from pursuing this question until now.

I attempted to run your code without success. Where do I put my-grep? Should it be a subroutine, internal to my Regina Rexx program? If it is to be a separate program, what name should it have?

I made it separate and named it my-grep.rex but that resulted in:
Looking for qux, made into 0 by wrapper script, expect no output.
sh: ./my-grep: not found

Regina couldn't find my-grep.

What should I do next?

Daniel B. Martin

catkin 06-07-2010 09:27 AM

So long since I used Rexx but isn't the display of non-zero return codes configurable? Ah yes -- TRACE O will do it according to this page.

makyo 06-07-2010 09:56 AM

Hi.

Yes, catkin's post is very useful.

Both trace and TRACE work. That seems to be better from a portability point of view. You can keep my suggestion in mind for a *nix-related suggestion.

Here's a modified version of my test script using trace:
Code:

#!/usr/bin/regina -

/*

# @(#) s3        Demonstrate results of TRACE O, rexx / regina.

*/

say
say ' Hello, world from regina.'

say
say " Version of regina:"
"regina -version"

say
say " Looking for bar."
"grep bar < data1"

trace n
say
say " Looking for qux, expect message, trace n(ormal)."
"grep qux < data1"

trace o
say
say " Looking for qux, trace o(ff)."
"grep qux < data1"

say
say " Looking for qux, 0 by wrapper script, expect no output, trace o."
"./my-grep qux < data1"

TRACE N
say
say " Looking for qux, 0 by wrapper script, expect no output, trace n."
"./my-grep qux < data1"

say
say " Real error results in a message, trace n."
"./my-grep a b c"

say
say " At the end."

producing:
Code:

% ./s3

 Hello, world from regina.

 Version of regina:
REXX-Regina_3.3(MT) 5.00 25 Apr 2004

 Looking for bar.
bar

 Looking for qux, expect message, trace n(ormal).
    23 *-* "grep qux < data1"
      +++ RC=1 +++

 Looking for qux, trace o(ff).

 Looking for qux, 0 by wrapper script, expect no output, trace o.

 Looking for qux, 0 by wrapper script, expect no output, trace n.

 Real error results in a message, trace n.
grep: b: No such file or directory
grep: c: No such file or directory
    41 *-* "./my-grep a b c"
      +++ RC=2 +++

 At the end.

Best wishes ... cheers, makyo

danielbmartin 06-09-2010 03:42 PM

Quote:

Originally Posted by catkin (Post 3995408)
So long since I used Rexx but isn't the display of non-zero return codes configurable? Ah yes -- TRACE O will do it according to this page.

Perfect! Thank you! SOLVED!

Daniel B. Martin


All times are GMT -5. The time now is 06:44 AM.