1

./run.sh:

1. cp=warmonger-1.0.0.jar
2. cmmd="java -server -D64 -Xms200m -Xmx200m 
     -Dlog4j.configurationFile=$WARMONGER_HOME/etc/log4j2.xml
     -classpath $cp warmonger.agent.WarmongerAgentApp"
3. echo $cmmd

execute results:

dataq.agent.DataqAgentApp -Xmx200m 
  -Dlog4j.configurationFile=/warmonger/etc/log4j2.xml 
  -classpath warmonger-1.0.0.jar

"warmonger.agent.WarmongerAgentApp" not appear.

I means if remove echo, java will be throw an exception: Couldn't find main class

Broderick
  • 41
  • 5
  • warmonger-1.0.0.jar it is already printing? What do you mean cp is not appearing? – kiran Biradar Aug 02 '18 at 07:33
  • 1
    The changes `cp=warmonger-1.0.0.jar` makes to the environment are lost once the shell finishes. – jww Aug 02 '18 at 08:50
  • @kiranBiradar cp's after not print, or remove echo, this command is wrong. – Broderick Aug 02 '18 at 22:18
  • Commands you want to execute shouldn't be put into string variables at all; better practice is to use an array or a function -- see [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050). There are lots of example scripts for starting Java programs that do this, granted -- but if you read the link you'll see why the pattern creates fragile, bug-prone code. – Charles Duffy Aug 02 '18 at 22:19

2 Answers2

0

You won't see $cp when you echo $cmmd, because the shell substitutes the value of the cp parameter (warmonger-1.0.0.jar) in the assignment to cmmd.

You can escape the dollar sign, or use single quotes if you don't want the shell to expand the parameter.

Engineero
  • 12,340
  • 5
  • 53
  • 75
ewindes
  • 790
  • 7
  • 17
0

Your shell script is CR-LF terminated (DOS/Windows ends of lines). Thus, from bash point of view, the cp variable contains warmonger-1.0.0.jar<CR> (notice the trailing <CR>).

When you echo the content of the cp variable, <CR> is echoed too which puts the cursor at the beginning of the line (CR = carriage return). echo then prints the remaining of the arguments at the beginning of the line.

You can see it in your output:

  • "java -server -D64 -Xms200m -Xmx200m" is overwritten with "warmonger.agent.WarmongerAgentApp"
  • which, in turn, is overwritten by some other command output ("dataq.agent.Dataq")

Solution: turn your DOS/Windows text file into a UNIX one. See this answer.

xhienne
  • 5,738
  • 1
  • 15
  • 34