简体   繁体   中英

wrong arguments are passed to .ksh

The arguments are not being passed as expected when I try to execute the following .ksh file.

ProcessLauncher.ksh:

/usr/java/jdk1.7.0_25/bin/java -Xmx256M $1 $2 $3 $4 $5 $6 $7 $8 $9 $10

This is the code I execute to invoke the above .ksh file:

CallingClass:

public static void main(String[] args) {
    String[] cmdline = {};
    cmdline = new String[]{"ksh", "../scripts/ProcessLauncher.ksh", com.myPackage.CalledClass.class.getName(), "SimpleDF", "1099"};
}

And this is the code which is executed after calling .ksh file:

CalledClass:

public static void main(String[] args) {
    System.out.println("Arguments passed: " + Arrays.toString(args));        
    if (args.length != 2) {
            System.out.println("Invalid arguments");
            System.exit(0);
    }
}

Expected result after executing the CallingClass#main() method:

Arguments passed: SimpleDF 1099

Actual result after executing the CallingClass#main() method:

Arguments passed: SimpleDF 1099 com.myPackage.calledClass

Invalid arguments

The fully qualified classname is being passed incorrectly as last argument. I am using JDK7u25 (32-bit) on SuSE Linux Enterprise Server (32-bit). However, when I remove the last two arguments from the java command in .ksh file (ie $9 and $10) it works fine and I get the expected result.

Can someone please explain what is going on here?

Give it a try with ${10} rather than $10 . The ksh man page states under Parameter expansion :

A positional parameter of more than one digit must be enclosed in braces.

However, a better way to do it may be to use the entire array:

/usr/java/jdk1.7.0_25/bin/java -Xmx256M "$@"

You can actually see what's going wrong in the following transcript:

pax> cat tst.ksh 
#!/usr/bin/ksh
echo " 1  = [$1]"
echo " 2  = [$2]"
echo " :"
echo " 9  = [$9]"
echo "10a = [$10]"
echo "11a = [$11]"
echo "10b = [${10}]"
echo "11b = [${11}]"

pax> tst.ksh a b c d e f g h i j k
 1  = [a]
 2  = [b]
 :
 9  = [i]
10a = [a0]
11a = [a1]
10b = [j]
11b = [k]

The multi-digit positional parameters without braces are being treated as single-digit ones with a trailing digit constant. In other words, $10 is being treated as ${1}0 . When you surround the 10 with braces, the correct result is obtained.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM