I have written the script run.sh below for calling a java class :
java -cp . Main $1 $2 $3 $4
Here is my java class (it just displays the args) :
public class Main {
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
}
}
This is working unless I try to pass a parameter containing a space. For example :
./run.sh This is "a test"
will display :
This
is
a
test
How could I modify my shell script and/or change my parameter syntax to pass the parameter "a test" unmodified ?
Like this:
java -cp . Main "$@"
You have to mask every parameter in the script as well:
java -cp . Main "$1" "$2" "$3" "$4"
Now parameter 4 should be empty, and $3 should be "a test".
To verify, try:
#!/bin/bash
echo 1 "$1"
echo 2 "$2"
echo 3 "$3"
echo 4 "$4"
echo all "$@"
and call it
./params.sh This is "a test"
1 This
2 is
3 a test
4
all This is a test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With