Anuj Kulkarni
Anuj Kulkarni

Reputation: 2199

Modify bash arguments and pass to java program

Is it possible to modify the arguments passed to a bash program ? and then pass them to a Java program ?

I know we can access all the arguments passed to a bash program by "$@" and I can pass them to Java program like java com.myserver.Program "$@". But is it possible to modify value of certain arguments inside "$@" and then call the above java program with "$@" ?

I also know you can use "$@[1]" to access the value of arguments but how can we iterate over them and change the value at the proper position ? I also know this :

for arg
do
....
done

But inside the do loop how can the value of argument be modified and then java program be called ?

Upvotes: 1

Views: 162

Answers (1)

konsolebox
konsolebox

Reputation: 75458

Iterate through every item in the positional parameters, then add them to another array, modifying it if needed while you do.

ARGS=()
for A in "$@"; do
    # Modify A then add it to args. 
    # A=${A//something/something} ## Just an example.
    ARGS+=("$A")
done

# Then call java:

java com.myserver.Program "${ARGS[@]}"

And since you're processing positional parameters, it would be simpler to use this form of for:

for A; do

Upvotes: 4

Related Questions