bashunixkshexit-codemks

korn vs bash exit code


I have the following problem:

The problem is in the following:

This works fine in KornShell (ksh) but doesn't work in bash:

typeset -i errorCode
errorCode=10
exit errorCode

I need to modify the script like this (note the $ sign):

typeset -i errorCode
errorCode=10
exit $errorCode

Is there a way to make the code to be compilable with bash without making the chnage I mentioned?

Thanks


Solution

  • you can also do this before executing the ksh script in bash

    #!/bin/bash
    exit ()
    {
        unset -f exit
        if expr match "$1" '^[-|+|0-9|.][.0-9]*$' &> /dev/null ; then
                exit "$1"
        fi
        exit
    }
    source ./the_ksh_script
    

    to override exit. This example is compatible with the original code, i.e. gives exit code of 0.

    Second example

    exit () {
     unset -f exit
     eval rc2=\$$1
     rc1=$1
     for rc in "$rc1" "$rc2"
      do
       if expr match "$rc" '^[-|+|0-9|.][.0-9]*$' &> /dev/null ; then
        exit "$rc"
       fi
      done
     exit
    }
    
    source ./the_ksh_script
    

    will produce the exit code 10, probably what the original script intends.