I am working in HP-UX (B.11.23) to LINUX (Red Hat Enterprise Linux Server release 6.5 - Santiago) migration project. There were several occurrences where HP-UX commands were not working properly in Linux. But, in following situation - I am facing some difficulties to find out an alternate solution.

Following piece of code is just an example from a script, but requesting you to understand the final outcome of the code.

## Assigning a text value in retval (bad path scenario)_
if [ $retval -eq 7 ]
     echo "I am in " $retval
exit $retval

Executing this code in LINUX...

LINUX $> ksh --version
version         sh (AT&T Research) 93u+ 2012-08-01
LINUX $> ksh -x test12.ksh
+ retval=ERROR
+ [ ERROR -eq 7 ]
+ exit ERROR
tpl06429dat05::edwprod::/home/edwprod >echo $?
# look here return code of above executed script is 0_

Now, have executed same code in HP_UX

UNIX $> uname -a
HP-UX ipeip06 B.11.23 U ia64 ******** unlimited-user license
UNIX $> strings /bin/ksh | grep Version | tail -2
@(#)Version 11/16/88
UNIX $> ksh -x test12.ksh
+ retval=ERROR
+ [ ERROR -eq 7 ]
test12.ksh[3]: ERROR: bad number
+ exit ERROR
test12.ksh[8]: ERROR: bad number
UNIX $> echo $?
# Look here, return code of above executed code is 1

In Linux, "exit" command is not executed at all as I have passes wrong parameter (string), but in HP-UX there is no problem - "exit" command with wrong parameter has been executed and tells us that it's a wrong command (echo $?) in that script. And this return code is very important as based on this return code ($?), we decide whether a script has been executed successfully (0) or not.

I understand, if we tweak the code, then problem can be resolved. But that's bit difficult here as I am dealing with almost 15K or more such scripts which are written in HP_UX. Is there any possibility to change something at OS level (changing packages/parameters).. What kind of system level changes needed?

On a different note, ksh versions are different and behaves differently. So, it's better if you avoid this answer.

  • 2
    What versions of ksh are you using on both machines? – Stephen Harris Aug 22 '16 at 17:55
  • 1
    Can't you fornat your code correctly, with real linebreaks? -eq works for numbers only, not for strings. – ott-- Aug 22 '16 at 18:03
  • 2
    I'd recommend you change the scripts anyway, if they're actually passing unquoted variables to test. – Jeff Schaller Aug 22 '16 at 18:20
  • 2
    What horrible code. The “UNIX” output isn't any more correct than the “LINUX” output. Linux does execute the exit command, it just interprets the bad argument differently — the solution is of course not to pass a bad argument. – Gilles Aug 22 '16 at 22:38
  • 1
    “UNIX” is meaningless. Unix is a family of operating systems, of which Linux is a subfamily. What variant of Unix is the one that isn't Linux? What version of ksh are you running on both? – Gilles Aug 22 '16 at 22:39

Your script ends

exit $retval

And you set retval to a string (whose first word is "ERROR:").

According to the specification for the exit builtin,

The exit utility shall cause the shell to exit with the exit status specified by the unsigned decimal integer n. If n is specified, but its value is not between 0 and 255 inclusively, the exit status is undefined.

"undefined" means that it's OK, and sometimes even expected, for different implementations to give different results.

Looking at your ksh versions, the one on HP-UX (11/16/88) gives exit status 1, while the one on your Linux installation (93u+) gives exit status 0. If you want the Linux system to behave more like the HP-UX one, you'll have to install a different ksh that behaves the old way, such as mksh or pdksh.

  • Thanks.. I think that is only option. Have tried with "bash" - it's working fine. – Santanu Aug 23 '16 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.