views:

240

answers:

2

From the wrapper shell scripts i am calling the Java program. I want the Unix shell script to pass all the arguments to java program except the EMAIL argument. HOW Can i remove the EMAIL argument and pass the rest of the arguments to the java program. EMAIL argument can come at any position.

valArgs()
{

    until [ $# -eq 0 ]; do
        case $1 in
            -EMAIL)
                MAILFLAG=Y
                shift
                break
                ;;
        esac
    done
}



main()

{

 valArgs "$@"

 $JAVA_HOME/bin/java -d64  -jar WEB-INF/lib/test.jar "$@"
+1  A: 

just to remove "-EMAIL" option right? i assume it doesn't come with extra params after "-EMAIL"

main(){
 args="$1"
 case "$1" in
    *-EMAIL*)
       args=${args/-EMAIL/}
 esac
 $JAVA_HOME/bin/java -d64  -jar WEB-INF/lib/test.jar "$args"
}
ghostdog74
args is assigned with first argument. then args=${args/-EMAIL/}what is the above statement trying to do.
Arav
if "-EMAIL" is found in $args, then remove it (substitute with blank)
ghostdog74
EMAIL argument can appear in first or second or... (Optional one). How can i change the above program to remove it and pass the rest to the program
Arav
then you use "$@"
ghostdog74
A: 

If you're using bash you could use the following snippet. The use of arrays helps get around issues which may occur if there are spaces in the positional arguments.

Remember that positional arguments passed in your original example only persist for the duration of the valArgs() call.


#!/bin/bash

main()
{
    # Build up arg[] array with all options to be passed
    # to subcommand.
    i=0

    for opt in "$@"; do
        case "$opt" in
        -EMAIL)
            MAILFLAG=Y
            ;;
        *)
            arg[i]="$opt"
            i=$((i+1))
            ;;
        esac
    done

    $JAVA_HOME/bin/java -d64  -jar WEB-INF/lib/test.jar "${arg[@]}"
}

main "$@"
Austin Phillips
I am using korn shell. will it work?
Arav
I replaced /bin/bash with /usr/bin/ksh on my Debian Lenny system and it worked for me.
Austin Phillips
what does this ${arg[@]} means? @ indicats the number of values in array? if i use echo "${arg[@]}" after main "$@" is it fine as a global variable. I want to call the function main and after that i need to call the java program with arguments
Arav
${arg[@]} expands the arg array in the same way that "$@" expands positional parameters with each parameter enclosed by quotes. eg It expands to "$arg[0]" "$arg[1]" etc. The use of arrays overcomes issues when there are spaces in the arguments. Use of arg as global after calling main is ok as arg has global scope.
Austin Phillips