1

I have script to run a jar, like shown below.

user="Tim Tom";
jarfile=./app.jar
SC_CD="java -jar -Xms512m -Xmx2048m -DUSER='$user' $jarfile"
nohup $SC_CD

And im geeting error message as

Error: Unable to access jarfile Tom

So, my understanding is, it is taking the word after space as jar name.

Therefore, my question is, How to pass param with space in it?

anij
  • 113
  • 1
  • 5
  • 1
    See http://mywiki.wooledge.org/BashFAQ/050 – Eric Renouf May 05 '16 at 12:12
  • 2
    Do you need to store the command in a variable? Could you just nohup java -jar -Xms512m -Xmx2048m -DUSER="$user" "$jarfile" instead of having those on different lines? – Eric Renouf May 05 '16 at 12:30
  • I agree fully with @EricRenouf; you would be much better off without stuffing your arguments into a parameter. Any workaround is going to be error-prone and, without excellent shell scripting skills, hard to debug. – Wildcard May 05 '16 at 19:37
  • Alternatively, if you don't mind reducing the portability of your product by depending on bash instead of /bin/sh, you could use bash arrays. – Wildcard May 05 '16 at 19:45

1 Answers1

1

based on Eric Renouf's comments on previous iteration of this answer on how nohupand bash conspire to thwart escaping spaces

user="Tim Toms"
jarfile=./app.jar
SC_CD="java -jar -Xms512m -Xmx2048m -DUSER='$user' $jarfile"
echo $SC_CD  > temp.sh
nohup bash temp.sh

If the point was to let that java run in the background, I might have just done something like

echo java -jar -Xms512m -Xmx2048m -DUSER=\'$user\' $jarfile | at now

and forgotten about nohup (so what if maybe there's some trash email to clean up)

==== old answer below, just so comments make sense. Forget below otherwise ====

While it seems your statements actually do call nohup with "Tim Tom" in single quotes, perhaps nohup does its system() call with a simple string instead of making an exec call, and making the string loses the quotes.

I just tested by putting an echo in front of the nohup

I am not really in a position to test it, but I suggest try making your first line into

user='"Tim Tom"'

as single quotes are supposed to prevent expansion and should pass the double quotes on to nohup

I'm basing this on making a test script like

ser="Tim Tom";
jarfile=./app.jar
SC_CD="java -jar -Xms512m -Xmx2048m -DUSER='$user' $jarfile"
echo nohup $SC_CD

then when I sh -x test.sh I get

+ user='Tim Tom'
+ jarfile=./app.jar
+ SC_CD='java -jar -Xms512m -Xmx2048m -DUSER='\''Tim Tom'\'' ./app.jar'
+ echo nohup java -jar -Xms512m -Xmx2048m '-DUSER='\''Tim' 'Tom'\''' ./app.jar
nohup java -jar -Xms512m -Xmx2048m -DUSER='Tim Tom' ./app.jar

But making the suggested change gives

+ user='"Tim Tom"'
+ jarfile=./app.jar
+ SC_CD='java -jar -Xms512m -Xmx2048m -DUSER='\''"Tim Tom"'\'' ./app.jar'
+ echo nohup java -jar -Xms512m -Xmx2048m '-DUSER='\''"Tim' 'Tom"'\''' ./app.jar
nohup java -jar -Xms512m -Xmx2048m -DUSER='"Tim Tom"' ./app.jar

Note that in both cases, echo shows that nohup should be getting at least the first level of quotes in its arguments. That's why I am suggesting adding an additional level

infixed
  • 887
  • That won't help here because the quotes inside the variable are just other characters in the string, they're not being syntactic to affect word splitting – Eric Renouf May 05 '16 at 14:23
  • I think the problem here is nohop doing string manipulation to make a system call. there should be a big difference if nohup is passed a quoted string with a space, as opposed to a simple argument with a space, but no quotes. It should be the difference inside nohup between system( "java -jar -Xms512m -Xmx2048m -DUSER=Tim Tom ./app.jar" ); and system( "java -jar -Xms512m -Xmx2048m -DUSER=\"Tim Tom\" ./app.jar"); – infixed May 05 '16 at 15:01
  • nohup doesn't do a system at all, it does execvp and passes all its arguments to that. Word splitting in bash has already thwarted you before it gets that far. So if you put -DUSER='"Tim Tom"' because the quotes there are just part of the string when that word splitting happens you are getting 2 arguments: -DUSER="'Tim and Tom"' and no amount of quoting will change that. – Eric Renouf May 05 '16 at 16:10
  • You can see that breaking up of the variables if you replace your echo with a printf that will show each argument separately, like doing printf '%s\n' nohup $SC_CD will show that Tim and Tom are different arguments to nohup regardless of the quoting you try to add – Eric Renouf May 05 '16 at 16:13
  • modified answer top-post style based on your comments – infixed May 05 '16 at 17:49
  • that nohup bash temp.sh works! thanks to all! :) – anij May 06 '16 at 09:23