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 "$@"

2 Answers 2

1

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"
}
Sign up to request clarification or add additional context in comments.

3 Comments

args is assigned with first argument. then args=${args/-EMAIL/} what is the above statement trying to do.
if "-EMAIL" is found in $args, then remove it (substitute with blank)
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
1

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 "$@"

3 Comments

I replaced /bin/bash with /usr/bin/ksh on my Debian Lenny system and it worked for me.
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
${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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.