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 
icecool10
  • 41
  • 2
  • 11
  • 1
    Regarding the `&` - see [this question](https://stackoverflow.com/questions/13338870/what-does-at-the-end-of-a-linux-command-mean). – andrewJames Jun 16 '20 at 21:56

1 Answers1

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.

alani
  • 12,573
  • 2
  • 13
  • 23
  • 1
    thanks @alaniwi for pointing out the wrongs and helping me learn . – icecool10 Jun 16 '20 at 22:03
  • 1
    @icecool10 I missed an issue. Please recheck edited version because I've added number 6 now. – alani Jun 16 '20 at 22:09
  • yes i just edited it locally .... need to bring the -D port option before jar thanks though – icecool10 Jun 16 '20 at 22:10
  • @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. – alani Jun 16 '20 at 22:14
  • 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. – Benjamin W. Jun 16 '20 at 22:14
  • @BenjaminW: point taken but it's just a list of port numbers, which are not going to contain whitespace etc. I didn't want to over-engineer, and wanted to stay as close to the OP's code as reasonably possible. But certainly for anything that could contain anything that might be a shell meta-character, I agree 100%. – alani Jun 16 '20 at 22:17
  • @alaniwi also a question , how can i read numbers from file and use it in the -Dserver.port variable ? I think it will complain that it is String and not integer . – icecool10 Jun 17 '20 at 13:57
  • just numbers like 8001 8002 8003 8004 @alaniwi – icecool10 Jun 17 '20 at 14:01
  • This may help: https://stackoverflow.com/questions/10929453/read-a-file-line-by-line-assigning-the-value-to-a-variable – alani Jun 17 '20 at 14:26