Script that works on one server does not work on another

Hello all,
The following script (sess.sh) runs fine and gives the correct output on a SunOS 5.9 ( default shell =/bin/ksh) but gives incorrect results on a SunOS 5.7 (default shell= /bin/sh). The script logs on to an oracle database and queries the total no of sessions running in the database and passes that number as the return code to the shell script.

sess_warning.sh
----------------
#!/bin/sh
sqlplus -s '/as sysdba' <<EOF
column scnt new_value scount
select count(*) scnt from v\$session;
exit scount
EOF
echo "sessions= $?"
exit

OS= SunOS 5.9, default shell =/bin/ksh ( gives correct result)
----------------------------------------------------------

scott@westpark> ./sess.sh 2>sess_err.log

  SCNT

----------
94

sessions= 94

OS= SunOS 5.7, default shell =/bin/sh ( gives incorrect result)
----------------------------------------------------------

$ ./sess.sh 2> sess_err.log

  SCNT

----------
315

sessions= 59

Am I missing something here?

Thanks

You missed this language: "the low-order eight bits (that is, bits 0377) of status are made available" on the exit system call man page. And sure enough...

$ echo "obase=8;315"  | bc
473
$ echo "obase=8;59"  | bc
73
$

You're just dropping the high bit from the exit code.

Thanks Perderabo for the quick reply but I have to admit that I could not understand your reply. Could you explain a bit more. What should I do to make the script work.

Thanks.

The exit code is only an 8-bit number. 315 is too big a number to fit into 8 bits.

Try this instead:

#!/bin/sh
echo sessions=`sqlplus -s '/as sysdba' <<EOF
column scnt new_value scount
select count(*) scnt from v\$session;
exit scount
EOF
`
exit

Thanks. I'll give it a try..

You should not use exit to return values - what if there are more than 256?
It's sqlplus problem:

#!/bin/sh
sessions = $(
sqlplus -s '/as sysdba' <<EOF
column scnt new_value scount
set head off
set feed off
set pages 0
select count(*) scnt from v\$session;
exit
EOF )
echo "sessions= $sessions"
exit

Thanks Jim, that's where I was heading but my post was rubbish in retrospect!! :o