Arav
Arav

Reputation: 5247

shell script passing subset of arguments

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

Upvotes: 2

Views: 2416

Answers (2)

Austin Phillips
Austin Phillips

Reputation: 15746

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

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342303

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"
}

Upvotes: 1

Related Questions