Re: expr exit status

2010-01-20 Thread Philip Rowlands

On Wed, 20 Jan 2010, salih k wrote:


add_num=`expr $int_num + 1 1/dev/null 21`

   if [ $? -ne 0 ]

some case exit status is 1552 for expr even though the argument is integer


This cannot be true, as the exit status exposed through the $? shell 
variable is limited to the range 0-255. For example,


$ sh -c 'exit 255'; echo $?
255
$ sh -c 'exit 1552'; echo $?
16

As Eric has (patiently) explained there are better ways in shell to test 
for an integer, so I'd suggest using those.


For reference, this is what expr will do with various inputs:

$ expr 5 + 1 /dev/null; echo $?
0

$ expr -1 + 1 /dev/null; echo $?
1

$ expr fish + 1 /dev/null; echo $?
expr: non-numeric argument
2


Cheers,
Phil




Re: expr exit status

2010-01-20 Thread salih k
On Wed, Jan 20, 2010 at 7:33 PM, Philip Rowlands p...@doc.ic.ac.uk wrote:

 On Wed, 20 Jan 2010, salih k wrote:

 add_num=`expr $int_num + 1 1/dev/null 21`

   if [ $? -ne 0 ]

 some case exit status is 1552 for expr even though the argument is integer


 This cannot be true, as the exit status exposed through the $? shell
 variable is limited to the range 0-255. For example,

 $ sh -c 'exit 255'; echo $?
 255
 $ sh -c 'exit 1552'; echo $?
 16

 As Eric has (patiently) explained there are better ways in shell to test
 for an integer, so I'd suggest using those.

 For reference, this is what expr will do with various inputs:

 $ expr 5 + 1 /dev/null; echo $?
 0

 $ expr -1 + 1 /dev/null; echo $?
 1

 $ expr fish + 1 /dev/null; echo $?
 expr: non-numeric argument
 2


 Cheers,
 Phil



Hi ,

Thanks,
Means if the exit status exceeding 255 we have to take the reminder of
modulus 256.

so if I f take the modulus 256 for all the exit status exceeding 255 the
result will be same as you said
$ sh -c 'exit 255'; echo $?
255
$ sh -c 'exit 1552'; echo $?
16

Here 51198 mod 256= 254

1552 mod 256=16



Now shall I explain again (sorry:))

 actually the issue is rare and never happened in unix .So am curious on
this.



Now my query is why these exit status (254 or 16) are coming during the
first run then it become correct value after rerun?and

*what does it mean by exit status 16 and 254?Whether this can do any thing
with expr or issue with *

*/dev/null etc?*



As pen man pages(sorry if it is repeating)

 b.info expr|more
Exit status:

  0 if the expression is neither null nor 0,
  1 if the expression is null or 0,
  2 if the expression is syntactically invalid,
  3 if an error occurred.

there is nothing defined if 3.Thats why am asking *whether I can use if [
$? -eq 1 -o $? -eq 2 -o $? -eq 3 ] instead of*

*if [ $? -ne 0 ].my expr version:5.97*

**

*In case of HP unix it specifies that for expr*


*1 Expression is null or zero.*

*2 Invalid expression.*

*   2 An error occurred while evaluating the expression.
Here the exit status for expr can be greater than 2 also.So there is a
meaning in if [ $? -ne 0 ].But in coreutils expr the man page says only 0 to
3.Can any one pls check and *



If i should not use if [ $? -eq 1 -o $? -eq 2 -o $? -eq 3 ] then i will go
ahead with case.But the current expr is a running codew so my manager need
explanation.thats why.If expr is not stable i have to change all the places
in script which uses expr.

If *this is the only way(ie expr is not stable) then i will go ahead with
case*



   case $int_num in( i belive i have to use *case $int_num *for more
safety)
  *[!0-9]*|'')

  echo nonumeric

  continue
 ;;
   esac

Here more testing effort is reqd thats why am sticking to expr.



Thanks,

Salih


Re: expr exit status

2010-01-20 Thread Philip Rowlands

On Wed, 20 Jan 2010, salih k wrote:


Now shall I explain again (sorry:))

actually the issue is rare and never happened in unix .So am curious on
this.


never happened in unix? Which OS and shell are you using?


Now my query is why these exit status (254 or 16) are coming during the
first run then it become correct value after rerun?and

*what does it mean by exit status 16 and 254?Whether this can do any thing
with expr or issue with *
*/dev/null etc?*


No it can't. The exit status you observe is a bug or a mistake in your 
code or analysis.



Thats why am asking *whether I can use if [ $? -eq 1 -o $? -eq 2 -o $? -eq 3 ]
instead of*

*if [ $? -ne 0 ].


No; the apparent exit status greater than 255 is a bug, or a mistake in 
your code or analysis.



If i should not use if [ $? -eq 1 -o $? -eq 2 -o $? -eq 3 ] then i will go
ahead with case.But the current expr is a running codew so my manager need
explanation.thats why.If expr is not stable i have to change all the places
in script which uses expr.


If it's important to explain your observed usage of expr then I'm not 
sure what to suggest; maybe run the whole script under strace and check 
what arguments for exit() expr is giving, which may hint at the cause of 
the problem.



Cheers,
Phil




Re: expr exit status

2010-01-20 Thread salih k
On Wed, Jan 20, 2010 at 8:29 PM, Philip Rowlands p...@doc.ic.ac.uk wrote:

 On Wed, 20 Jan 2010, salih k wrote:

  Now shall I explain again (sorry:))

 actually the issue is rare and never happened in unix .So am curious on
 this.


 never happened in unix? Which OS and shell are you using?

  Now my query is why these exit status (254 or 16) are coming during the
 first run then it become correct value after rerun?and

 *what does it mean by exit status 16 and 254?Whether this can do any thing
 with expr or issue with *
 */dev/null etc?*


 No it can't. The exit status you observe is a bug or a mistake in your code
 or analysis.


 Thats why am asking *whether I can use if [ $? -eq 1 -o $? -eq 2 -o $? -eq
 3 ]
 instead of*

 *if [ $? -ne 0 ].


 No; the apparent exit status greater than 255 is a bug, or a mistake in
 your code or analysis.


 If i should not use if [ $? -eq 1 -o $? -eq 2 -o $? -eq 3 ] then i will go
 ahead with case.But the current expr is a running codew so my manager need
 explanation.thats why.If expr is not stable i have to change all the
 places
 in script which uses expr.


 If it's important to explain your observed usage of expr then I'm not sure
 what to suggest; maybe run the whole script under strace and check what
 arguments for exit() expr is giving, which may hint at the cause of the
 problem.


 Cheers,
 Phil



  Hi,
 Well ,I have three queries now,

  1.I am waiting for  answer whether I can use if [ $? -eq 1 -o $?
-eq 2 -o $? -eq 3 ]!!! as only these values are meaningful if we
  are checking for alphanumic cases(?)

 2.The exit status you observe is a bug or a mistake in your code or
analysis I did not get.
Ans-see below log trace

* Debug msg1 File 1 has isnum 28 and pstat 0
Debug msg2 File 1 has isnum 28
Debug msg3 File 1 has int_num 28 has ps 0
Debug msg3.1 File 1 has int_num1 28 has ps 0
Debug msg4.new1 File has expr pstat 51198 int_num is 28
Debug msg4 File 1 with paddnum has expr pstat 0
Debug msg5 File 1 with paddnum1 29 pstat1 0
File 1 has alpha-numeric value in header record for total shipments !!!
aborting
Upload of file 1 aborting

DATE: 07/01/2010 TIME: 21:27:59
-*
*...
Debug msg2 File 2  has isnum 1
Debug msg3 File 2  has int_num 1 has ps 0
Debug msg3.1 File  2 has int_num1 1  has ps 0
Debug msg4.new1 File has expr pstat 1552 int_num is 1---3!!!
Debug msg4 File 2  with paddnum has expr pstat 0---inside the if condition i
gave the same expr smt result now zero!!!*
*This is am asking about stability!!!*
This means that status is 3 outside(even if the argument to expr is 28 or
1) the if condition then it became zero inside!!!
see the below script for the trace


  ***isnum=`awk -F$delim '$1==BH{print $5}' $fil`*

*#awk -F$delim '$1==BH{print $5}' $fil |read isnum*

*  log_msg Debug msg1 File $fil has isnum $isnum and pstat $? *

*isnum=${isnum:-0}*

*  log_msg Debug msg2 File $fil has isnum $isnum *

*int_num=`echo -e $isnum | cut -f1 -d'.'`*

*  log_msg Debug msg3 File $fil has int_num $int_num has ps $? *

*  int_num1=`echo $isnum | cut -f1 -d'.'`*

*  log_msg Debug msg3.1 File $fil has int_num1 $int_num1 has ps $?*

*add_num=`expr $int_num + 1 1/dev/null 21`*

*  kms=$?*

*  if [ $kms -ne 0 ]*

*then*

*   log_msg Debug msg4.new1 File has expr pstat $kms int_num is
$int_num*

*padd_num=`expr $int_num + 1 1/dev/null 21`*

*  log_msg Debug msg4 File $fil with  paddnum has expr pstat $? *

*  padd_num1=`expr $int_num + 1 2/dev/null`*

*  log_msg Debug msg5 File $fil with paddnum1 $padd_num1 pstat1 $?*

*errmsg File `basename $fil` has alpha-numeric value in
header record for total shipments !!! aborting*

*sleep 1*

*log_msg Upload of $fil aborting*

*log_msg *

*out_rep $repfil \nFile `basename $fil` has alpha-numeric
value in header record for total shipments !!! aborting*

*continue*

*fi*

**

3.As you suggested(thanks)check what arguments for exit() expr is giving

=Frankly i dont know how to check the same I will be thankful if you please
let me know the way to check the same



Thanks,

Salih


Re: expr exit status

2010-01-20 Thread Eric Blake
According to salih k on 1/20/2010 5:55 AM:
   Hi,
  Well ,I have three queries now,

[please trim your replies down to relevant portions you are replying to -
we don't need to read the same text over and over again]

 
   1.I am waiting for  answer whether I can use if [ $? -eq 1 -o $?
 -eq 2 -o $? -eq 3 ]!!! as only these values are meaningful if we
   are checking for alphanumic cases(?)

NO.  We have REPEATEDLY told you that checking $? -eq 3 is NOT a portable
way to check for failure.  GNU coreutils' expr can fail with a status 
128 if it was terminated by a signal.  Therefore, checking merely against
status 3 is insufficient to catch the case of all possible statuses larger
than 2.

 Debug msg4.new1 File has expr pstat 51198 int_num is 28

And you've already posted that, but without any strace proof that your
system or shell is not the culprit.

 3.As you suggested(thanks)check what arguments for exit() expr is giving
 
 =Frankly i dont know how to check the same I will be thankful if you please
 let me know the way to check the same

If you are on a Linux system, then 'strace expr $int_num + 1' will give
you a (very verbose) dump of every system call, including which value
exit() was called with.  In all likelihood, it will have been exit(0), in
which case, if $? is corrupted, it is a bug in your shell and not in expr.

-- 
Don't work too hard, make some time for fun as well!

Eric Blake e...@byu.net



signature.asc
Description: OpenPGP digital signature


Re: expr exit status

2010-01-20 Thread Eric Blake
According to Eric Blake on 1/20/2010 6:01 AM:
 3.As you suggested(thanks)check what arguments for exit() expr is giving

 =Frankly i dont know how to check the same I will be thankful if you please
 let me know the way to check the same
 
 If you are on a Linux system, then 'strace expr $int_num + 1' will give
 you a (very verbose) dump of every system call, including which value
 exit() was called with.  In all likelihood, it will have been exit(0), in
 which case, if $? is corrupted, it is a bug in your shell and not in expr.

Another thing to try would be running your script under 'sh -vx script',
to get output of exactly what the shell is evaluating at the time.  In
fact, that might be easier to use than strace.  But whatever you do, trim
traces down to the relevant portion, rather than sending large logs to the
list.  And please consider attaching (not pasting inline) unmangled (and
not post-edited) source and output; your insistence on inserting * inside
snippets is making it hard to determine where your snippets start and end,
and draws into suspicion whether you are making a mistake where the script
ran something different than what your email shows.

-- 
Don't work too hard, make some time for fun as well!

Eric Blake e...@byu.net



signature.asc
Description: OpenPGP digital signature


Re: expr exit status

2010-01-20 Thread salih k
On Wed, Jan 20, 2010 at 9:11 PM, Eric Blake e...@byu.net wrote:

 According to Eric Blake on 1/20/2010 6:01 AM:
  3.As you suggested(thanks)check what arguments for exit() expr is
 giving
 
  =Frankly i dont know how to check the same I will be thankful if you
 please
  let me know the way to check the same
 
  If you are on a Linux system, then 'strace expr $int_num + 1' will give
  you a (very verbose) dump of every system call, including which value
  exit() was called with.  In all likelihood, it will have been exit(0), in
  which case, if $? is corrupted, it is a bug in your shell and not in
 expr.

 Another thing to try would be running your script under 'sh -vx script',
 to get output of exactly what the shell is evaluating at the time.  In
 fact, that might be easier to use than strace.  But whatever you do, trim
 traces down to the relevant portion, rather than sending large logs to the
 list.  And please consider attaching (not pasting inline) unmangled (and
 not post-edited) source and output; your insistence on inserting * inside
 snippets is making it hard to determine where your snippets start and end,
 and draws into suspicion whether you are making a mistake where the script
 ran something different than what your email shows.

 --
 Don't work too hard, make some time for fun as well!

 Eric Blake e...@byu.net


--
Hi ,
I am not sure when the issue will happen again(so debugger not much useful
now as the issue not coinsistant) but the fix is required now.What are the
output obtained during last run that I already explained.

As you confirmed the exit status of expr can be 0,1,2,3 and 3 in expr
version:5.97,even if the i ma using the expr only for  a alphanumeric
check.So ibelive i have to go ahead with case statement(it will be a tough
time) and i belive then the linux mannual for expr need to b e modified as
it explains exit status only 0,1,2,3

Please let me know whether i have to attach the script(i am not attaching as
it may be repeating)
Is there any way to check the old traces.Because it happen on jan7

Thanks,
Salih


Re: expr exit status

2010-01-19 Thread Alfred M. Szmidt
   What are all the exit statuses I need to check just after expr
   command?  Is it only need to check 1 or 2 or 3 for fail condition
   and zero for success ?else pease specify

You only need to check for non-zero exit codes for failure.




Re: expr exit status

2010-01-19 Thread salih k
On Tue, Jan 19, 2010 at 3:37 PM, Alfred M. Szmidt a...@gnu.org wrote:

   What are all the exit statuses I need to check just after expr
   command?  Is it only need to check 1 or 2 or 3 for fail condition
   and zero for success ?else pease specify

 You only need to check for non-zero exit codes for failure.

---
Hi,
But some case exit status is 1552 for expr even though the argument is
integer .Initialy i checked
if [ $?-ne 0 ],but this shows the above error that why i am planning to use
kms=$?
if [ $kms -eq 1 -o $kms -eq 2 -o $kms -eq 3 ]
instaed of
if [ $kms -ne 0 ] is it fine or will it make issues.
 The mannual says
info expr|more
Exit status:
  0 if the expression is neither null nor 0,
  1 if the expression is null or 0,
  2 if the expression is syntactically invalid,
  3 if an error occurred.
Regards,
Salih


Re: expr exit status

2010-01-18 Thread Mike Frysinger
On Tuesday 19 January 2010 02:11:14 salih k wrote:
 What are all the exit statuses I need to check just after expr command?
 Is it only need to check 1 or 2 or 3 for fail condition and zero for
  success ?

please read the documentation -- `man expr`
-mike


signature.asc
Description: This is a digitally signed message part.