HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- expr grep question
Operating System - HP-UX
1837800
Members
8567
Online
110120
Solutions
Forums
Categories
Company
Local Language
back
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
back
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- 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
05-19-2006 06:13 AM
05-19-2006 06:13 AM
expr grep question
hi,
i execute the following statement and then see its output
echo $(expr " 50 * 60 "|bc ) | grep 'syntax'
echo $?
return 1 because it failed to find the word syntax.
But i get the same output for this too,
echo $(expr " abc * 60 "|bc ) | grep 'syntax'
echo $?
returns 1. ( but abc*60 throws error msg saying "syntax error on line 1, teletype" )
whats wrong ?
i execute the following statement and then see its output
echo $(expr " 50 * 60 "|bc ) | grep 'syntax'
echo $?
return 1 because it failed to find the word syntax.
But i get the same output for this too,
echo $(expr " abc * 60 "|bc ) | grep 'syntax'
echo $?
returns 1. ( but abc*60 throws error msg saying "syntax error on line 1, teletype" )
whats wrong ?
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2006 06:25 AM
05-19-2006 06:25 AM
Re: expr grep question
The most likely thing I can think of is that the error message "syntax error on line 1" is going to the standard error (file descriptor 2) and not standar out (file descriptor 1).
The pipe works only with standard out I think.
Try this:
echo $(expr "abc*60"|bc) 2>&1 | grep 'syntax'
echo $?
And see if you now get the desired results.
The pipe works only with standard out I think.
Try this:
echo $(expr "abc*60"|bc) 2>&1 | grep 'syntax'
echo $?
And see if you now get the desired results.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2006 06:36 AM
05-19-2006 06:36 AM
Re: expr grep question
Patrick is correct; errors in UNIX go to stderr (file descriptor 2) and grep in looking at stdin (fdes 1). In any event, you are going about this all wrong. Why not get the status of the bc command directly:
#!/usr/bin/sh
A=50
B=60
C=$(echo "${A} * ${B}" | bc)
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "C = ${C}"
else
echo "Bc failed; status = ${STAT}" >&2
fi
exit ${STAT}
You don't need both expr and bc; bc will work with both integers and floting-point values so it's probably a better choice.
#!/usr/bin/sh
A=50
B=60
C=$(echo "${A} * ${B}" | bc)
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "C = ${C}"
else
echo "Bc failed; status = ${STAT}" >&2
fi
exit ${STAT}
You don't need both expr and bc; bc will work with both integers and floting-point values so it's probably a better choice.
If it ain't broke, I can fix that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2006 09:08 AM
05-19-2006 09:08 AM
Re: expr grep question
Not sure what you are trying to get at, so I'm purely guessing:
# echo $(expr " abc * 60 "|bc ) | grep 'syntax'
the above throws a syntax error because "abc" is undefined and if it were then put a $ sign before it to access its value:
# abc=10
# echo $(expr " $abc * 60 "|bc ) | grep 'syntax'
And if you wanted to switch stdout and stderr around then do:
# abc=10
# echo $(expr " $abc * 60 "|bc ) 1>&2 2>&1| grep 'syntax'
hope it helps!
# echo $(expr " abc * 60 "|bc ) | grep 'syntax'
the above throws a syntax error because "abc" is undefined and if it were then put a $ sign before it to access its value:
# abc=10
# echo $(expr " $abc * 60 "|bc ) | grep 'syntax'
And if you wanted to switch stdout and stderr around then do:
# abc=10
# echo $(expr " $abc * 60 "|bc ) 1>&2 2>&1| grep 'syntax'
hope it helps!
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP