Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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).
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.
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
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 ...".
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.