Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I encountered an issue with grep command, the return value is wrong(not 0 or 1 or 2).
to reproduce it, i have 3 small testing files in /tmp directory:
1. x.sh
2. myparsers.sh
3. tt
x.sh will call myparsers.sh, which processes tt file
1. cat x.sh
Code:
#!/usr/bin/ksh
i=0
while [ 1 ]
do
echo "Loop: $i"
# using ksh -x here to show the return value from grep command
ksh -x /tmp/myparser.sh > /tmp/parser_out_$i
[ $? -ne 0 -o ! -s "/tmp/parser_out_$i" ] &&
{
echo "fail\n"
break
}
# remove temp files
if [ $(( i % 50 )) -eq 0 ]; then
rm -f /tmp/parser_out_*
fi
i=$((i+1))
done
2. cat tt
Code:
#DATAFILES
ORCL:ATAVOL1
ORCL:ATAVOL2
3. cat myparser.sh
Code:
#!/bin/ksh
found=0
cat tt |
while read aline
do
if [ "$found" -eq 0 ]; then
echo $aline | grep "#DATAFILES" >/dev/null
if [ $? -eq 0 ]
then
found=1
fi
else
echo $aline
fi
done
exit 0
all three files are in /tmp directory. run /tmp/x.sh, sooner or later(a few minutes or a dozen minutes) it will fail at the following command:
The return value here from grep is 12276, sometimes are 8224, 11552, or some other large numbers, and this cause my problem to fail, i tried egrep, same error.
but if I remove the ">/dev/null" in myparsers.sh file, I don't see the failure.
my linux is Red Hat Enterprise Linux Server release 5.2 (Tikanga)(64 bit). I don't see the same problem in redhat release 4 or redhat 5(no update).
not sure if anybody reported this bug and what the solution for me here, as i have many >/dev/null statements in my scripts.
really appreciate your help.
Last edited by cui_jenny001; 05-07-2009 at 07:11 PM.
Your post is difficult to read because you didn't put the code in code tags.
Example:
Code:
echo 'foo' > my-file
Anyway, instead of redirecting stdout from grep to /dev/null, you might want to just give it the -q switch -- that's what it is there for. See the grep(1) manpages for details.
I changed to use -q option, it causes other code to break and I was told not to use -q for portability reason, we should use /dev/null, as mentioned in grep man page:
from man page
-q, --quiet, --silent Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.
-s, --no-messages Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, traditional grep did not conform to POSIX.2, because traditional grep lacked a -q option and its -s option behaved like GNU grep's -q option. Shell scripts
intended to be portable to traditional grep should avoid both -q and -s and should redirect output to /dev/null instead.
but since ">/dev/null" causes grep to return a wrong value(intermittent), what else I can do to avoid it or what could be the root cause from my testing code?
One option is to use grep -q 'foo' on Linux systems, and use grep 'foo' >/dev/null on e.g. HP-UX systems.
Also, if you're concerned with portability I would suggest using sh instead of ksh.
-------
If you want to get to the root cause of the issue, you need to start eliminating possibilities. Take it out of the while loop and try to reproduce the problem with just grep and a redirection to /dev/null. If you can't reproduce it that way, then that should tell you something.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.