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 res开发者_JS百科t 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 "$@"
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"
}
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 "$@"
精彩评论