Question

I have the following problem:

  • I have some test scripts that previously were run with ksh (from MKS Toolkit)
  • The scripts will need to remain unmodified (not a single character will be changed).
  • I will use bash (from Mingw/Msys) to run these scripts.

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

Was it helpful?

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.

OTHER TIPS

Assuming that your code is in a file named foo.sh, you can wrap it with process substitution. For example:

bash <(sed 's/errorCode$/$&/' foo.sh)

Bash interprets the modified code as read from the file descriptor, and the exit status of the subshell is set as expected.

$ echo $?
10
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top