Google Answers Logo
View Question
 
Q: Shell Scripting Problem (involves Java) ( No Answer,   1 Comment )
Question  
Subject: Shell Scripting Problem (involves Java)
Category: Computers > Programming
Asked by: davidk-ga
List Price: $7.00
Posted: 29 Nov 2002 22:07 PST
Expires: 30 Nov 2002 16:47 PST
Question ID: 116655
To start, here is a simple Java program to print the command-line
args:

public class PrintArgs {
  public static void main(String args[]) {
    for (int i = 0; i < args.length; i++) {
      System.out.println(args[i]); 
    }
  }
}

And here is a simple shell script which invokes it:

#!/bin/sh
java PrintArgs $*

The problem comes when the script is invoked with arguments grouped
together in double quotes, as follows:
./printArgs.sh Arg1 "Arg2 Arg3"

produces the following undesirable result:
Arg1
Arg2
Arg3

(because $* strips away the quotes) 
the desired result is:
Arg1
Arg2 Arg3

(directly calling java, i.e. java PrintArgs Arg1 "Arg2 Arg3", DOES
produce the desired result, so the problem lies in the shell script)

Meanwhile:
./printArgs.sh Arg1 \"Arg2 Arg3\"

produces the following (also undesirable) result:
Arg1
"Arg2
Arg3"

I've been racking my brains out trying every combination of things to
get this to work: escaped quotes, escaped spaces, "$*", "$@", etc.
etc. I need help badly! I've tried many things, so if possible, please
verify that the answer works before responding.

Thanks,
David

Request for Question Clarification by answerguru-ga on 30 Nov 2002 00:36 PST
Hi davidk-ga,

I was just curious if you would accept a modification to your main()
method to solve this particular problem?

answerguru-ga
Answer  
There is no answer at this time.

Comments  
Subject: Re: Shell Scripting Problem (involves Java)
From: msypkens-ga on 30 Nov 2002 10:55 PST
 
Change your shell script to:

#!/bin/sh
java PrintArgs "$@"

See http://www.gnu.org/manual/bash-2.05a/html_mono/bashref.html#SEC25
for the explanation.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy