0

New to shell scripting, trying to run a java program in parallel with port specified as input. For example ./Test.sh 8080 8081 --> Desired result would be to run script twice with 2 ports . i think " & " is to make it run in PARALLEL ?

Help/ guidance would be appreciated.

#!/bin/bash
    
PORT=$*;

if [ $# -eq 0 ]
  then
    echo "No arguments supplied"
fi

for i in $PORT
  do
     java -DpropertySource=~/Downloads/app.properties -jar APP.jar                 
                -Dserver.port="$PORT"  &
 done 
1
  • 1
    Regarding the & - see this question. Commented Jun 16, 2020 at 21:56

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

thanks @alaniwi for pointing out the wrongs and helping me learn .
@icecool10 I missed an issue. Please recheck edited version because I've added number 6 now.
yes i just edited it locally .... need to bring the -D port option before jar thanks though
@icecool10 Fair enough. I wasn't sure about the specifics of the java command line itself; all the things I've pointed out are general shell scripting issues. There is a general convention of placing the option switches (starting with -) before the positional arguments (in this case the APP.jar), so what you say makes sense, but not all commands strictly enforce that, depending how they are doing their argument parsing.
It should really be ports=("$@") and then for i in "${ports[@]}". Or you can skip it altogether: for i by default iterates over "$@". The suggested method is flattening the input parameters and then expands that string unquoted, which can lead to all kinds of troubles.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.