This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Execute shell with space-contained arg in Linux Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Execute shell with space-contained arg in Linux" Watch "Execute shell with space-contained arg in Linux" New topic
Author

Execute shell with space-contained arg in Linux

Zee Ho
Ranch Hand

Joined: Jul 20, 2004
Posts: 128
Ranchers, When I execute the java program (described in a shell)in linux machine with white-space contained argument, the prgram received more arguments than expect, but I have already enclose the argument using double quote.

I use the command like this

deploy.sh "argu ment 1".

but the program prompt it receive 3 argument than 1.

in deploy.sh


#!/bin/sh

. ./set_classpath.sh

date >> ../../logs/cif_deployment.out
date >> ../../logs/cif_deployment.err

java -Xbootclasspath/p:lib/xalan-2.6.0.jar:lib/xercesImpl-2.6.2.jar:lib/xml-apis-1.2.01.jar -Dorg.xml.sax.parser=org.xml.sax.helpers.XMLReaderAdapter -Dorg.xml.sax.driver=org.apache.xerces.parsers.SAXParser -Dlog4j.configuration=log4j_deployment.properties -classpath $CIF_CLASSPATH com.core.cif.deploy.DeployTool $1 $2 $3 >> ../../logs/cif_deployment.out 2>> ../../logs/cif_deployment.err


is there any mistake I make?


SCJP 1.4<br />SCWCD 1.3<br />SCJD<br />SCBCD<br />IBM Xml Cert in progress
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

The problem is caused by the shell script stripped off your double quote.
To make it work you have to put the double quote inside your script when calling the java class.



The weakness of above script is it will wrapped all your arguments into one,
or you can code it like before


If you are VERY SURE there will always be 3 arguments, because is you do not pass enough arguments..the double quotes in the above script will pass empty string to your java program as argument...so your java program will always have 3 arguments.

I hope that make sense.
Zee Ho
Ranch Hand

Joined: Jul 20, 2004
Posts: 128
great, thx very much.

It is really a hard time for me to work on linux.
 
 
subject: Execute shell with space-contained arg in Linux
 
Similar Threads
Automatic execution of java file using bat or something else
How to roll over catalina.out in tomcat
Downloading log files
What did I do wrong?
to set environment var. with java program