There are a few bugs here:
1) $1
will only contain the first parameter. You will need $*
to contain more than one. (And given that you want the variable to contain multiple ports, it would then be more helpful to call the variable PORTS
.)
2) You cannot have the whitespace around the =
in a variable assignment in bash.
3) You are looping over i
but not using that variable inside the loop. Where you have -Dserver.port="$PORT"
you should instead use your loop variable i
.
4) You are missing a line continuation character \
at the end of the java ...
line (ensure that there is no whitespace after it).
5) The command separator ;
at the end of the first line is redundant (although it does not actually harm).
6) Where you are testing for wrong usage, the script will issue the warning but carry on regardless. You need to put an exit
statement there. It is good practice to give a non-zero exit value in the event of a failure, so exit 1
is suggested here.
Putting these together:
#!/bin/bash
PORTS=$*
if [ $# -eq 0 ]
then
echo "No arguments supplied"
exit 1
fi
for i in $PORTS
do
java -DpropertySource=~/Downloads/app.properties -jar APP.jar \
-Dserver.port="$i" &
done
Regarding the &
, it will launch the command in the background, so that execution of the script will continue (including then reaching the end and exiting) while the command that was launched may still be running. So yes, your java instances listening on the different ports will then be running in parallel.