- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: $? -eq 0 after grep only works if set +x is in...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2007 01:58 AM
03-21-2007 01:58 AM
I have a REALLY weird problem with a shell script on HP-UX 11.23 Itanium.
In the script below, $? -eq 0 will only work after the grep if I interpose set +x. As soon as I comment out or delete set +x, $? -eq 0 will always return false.
Points will be assigned for helpful answers.
Also, I could not find the manpage explaining the difference between /sbin/sh and /usr/bin/sh. I would have thought /usr/bin/sh is "better", but /sbin/sh is larger, any ideas on this one? Otherwise I will post it as a separate question.
Thanks, Christian
#!/usr/bin/sh
#
# findlogs.sh - Search EATE logfiles for interesting information
#
# Author : Christian Deutsch
# Created : 21. 3.2007
# Modified: 21. 3.2007
#
# Get the name of this shell-script
SCRIPT=`basename $0`
NEWER=
STRING=
while [ ! -z "$1" ] # Parse parameters
do
case "$1" in
--newer) shift; NEWER=$1 ;;
--string) shift; STRING=$1 ;;
*) echo "Usage: $SCRIPT [--newer
esac
shift
done
findlogs() # Find testcase logs (journal files) for the specified testcase name
{
count=0
fails=0
# rm -f /tmp/logs.$1
for dir in $DIRS
do
grep -q "$STRING" ${dir}/journal
#set +x
if [ $? -eq 0 ]
then
count=`expr $count + 1`
echo $dir
# echo $dir >> /tmp/logs.$1
grep \|FAIL ${dir}/journal
if [ $? -eq 0 ]
then
fails=`expr $fails + 1`
grep this_hostname $dir/condata | tail -1 | cut -d : -f2
fi
fi
done
echo
echo "Found $count files, $fails files with one or more fails in them."
}
# Main
cd $LATEST_EATE_REPORT_DIR
if [ "$NEWER" ]
then
DIRS=`find . -newer $NEWER -type d | grep -v -e ^.$ -e ./.executions`
else
DIRS=*
fi
findlogs
# eof
Solved! Go to Solution.
- Tags:
- exit status
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2007 02:08 AM
03-21-2007 02:08 AM
Re: $? -eq 0 after grep only works if set +x is in between?? (Points!)
Hi folks,
I have a REALLY weird problem with a shell script on HP-UX 11.23 Itanium.
In the script below, $? -eq 0 will only work after the grep if I interpose set +x. As soon as I comment out or delete set +x, $? -eq 0 will always return false.
Points will be assigned for helpful answers.
Also, I could not find the manpage explaining the difference between /sbin/sh and /usr/bin/sh. I would have thought /usr/bin/sh is "better", but /sbin/sh is larger, any ideas on this one? Otherwise I will post it as a separate question.
Thanks, Christian
#!/usr/bin/sh
#
# findlogs.sh - Search EATE logfiles for interesting information
#
=
grep -q "$STRING" ${dir}/journal
#set +x
if [ $? -eq 0 ]
then
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2007 02:09 AM
03-21-2007 02:09 AM
Re: $? -eq 0 after grep only works if set +x is in between?? (Points!)
I normally assign the $? to a variable, rather than using it in the if statement.
grep \|FAIL ${dir}/journal
a=$?
echo "[$a]"
if [ $a -eq 0 ]
.
.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2007 02:12 AM
03-21-2007 02:12 AM
SolutionThe difference is that /sbin/sh is statically linked, meaning that all libraries necessary for it to function are linked into the executable. The /usr/bin/sh is dynamically linked, meaning it loads libraries from /usr/lib as they are needed.
This is important because /sbin/sh is root's default shell, and MUST remain roots default shell, because the shell must be usable BEFORE the /usr filesystem is mounted. This is why single-user mode works when you only have / available.
This is also why you will break the system if you ever change root's default shell to something other than /sbin/sh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2007 02:12 AM
03-21-2007 02:12 AM
Re: $? -eq 0 after grep only works if set +x is in between?? (Points!)
Your observation does seem odd. You might also try:
# if [$? = "0" ]
As for '/usr/bin/sh' and '/sbin/sh' they are both the POSIX shell. THe difference is that '/usr/bin/sh' uses dynamic libraries whereas those for '/sbin/sh' are statically linked. This is the reason that you NEVER use anything but '/sbin/sh' for the root account, since during startup '/usr' isn't mounted!
The value of the '/usr/bin/sh' lies in its dynamic libraries. Their use reduces the overall memory footprint for multiple users.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2007 02:16 AM
03-21-2007 02:16 AM
Re: $? -eq 0 after grep only works if set +x is in between?? (Points!)
grep -q "$STRING" ${dir}/journal
#set +x
if [ $? -eq 0 ]
Note that you have "set +x" immediately before your test of ${?}. It is simply returning the status of the last statement/command -- namely the set itself.
What you should do is this:
grep -q "xxx" filea
STAT=${?} # capture the status immediately
set +x
if [[ ${STAT} - eq 0 ]]
Capturing ${?} in a variable is always a good technique because you then have it for things like error messages as well as evaluation.
Both /usr/bin/sh and /sbin/sh are fully functional and functionally equivalent versions of the POSIX shell. The difference is that /sbin/sh is a statically linked version of the shell so that no shared libraries are needed. Ask yourself, when in single-user mode and when only /stand and / are mounted, how would the startup scripts which run under a shell execute when the required shared libraries are in a filesystem that hasn't yet been mounted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2007 02:21 AM
03-21-2007 02:21 AM
Re: $? -eq 0 after grep only works if set +x is in between?? (Points!)
don't set the set +x between the command and the check:
look at this:
oracle@blah:/home/oracle$ cd /oracle
oracle@blah:/oracle$ echo $?
0
oracle@blah:/oracle$ cd /oracle
oracle@blah:/oracle$ set +x
oracle@blah:/oracle$ echo $?
0
oracle@blah:/oracle$ cd /aslkdjf
sh: /aslkdjf: not found.
oracle@blah:/oracle$ echo $?
1
oracle@blah:/oracle$ cd /asldjf
sh: /asldjf: not found.
oracle@blah:/oracle$ set +x
oracle@blah:/oracle$ echo $?
0
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2007 02:26 AM
03-21-2007 02:26 AM
Re: $? -eq 0 after grep only works if set +x is in between?? (Points!)
I'll close the post now.
Thanks for taking the time to answer, Christian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2007 02:28 AM
03-21-2007 02:28 AM