The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem when passing argument to a shell script sumesh.abraham Shell Programming and Scripting 9 12-13-2006 12:07 PM
Problem with Argument Passing A_Rod Shell Programming and Scripting 4 09-13-2006 12:47 PM
argument parsing... midhun_u Shell Programming and Scripting 1 07-25-2006 11:38 AM
command line argument parsing rmjoe UNIX for Dummies Questions & Answers 1 07-28-2005 04:39 PM
shell script argument parsing rmjoe Shell Programming and Scripting 1 07-28-2005 04:37 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-18-2007
fabulous2 fabulous2 is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 30
problem with spaces and argument parsing

[Preliminaries: the discussion below will refer to a Java class named HelloWorld. Its totally irrelevant to this posting what that class does, since the problem that I describe has to do with the shell, not Java. Its just that the problem arose for me while doing some coding, and I could not think of a more universal illustration.

Nevertheless, if you need something concrete in order to duplicate my actions, then the source file HelloWorld.java that I used contained the text
Code:
public class HelloWorld {
	public static void main(String[] args) {
		System.out.println("Welcome, master");
	}
}
and I compiled using
javac HelloWorld.java
]



Suppose that I execute the following command directly from the shell:
java -XX:OnError="gdb - %p" HelloWorld
Then it works perfectly fine, as expected.

Now suppose that I create the following shell script:
Code:
#!/bin/sh
echo java  -XX:OnError=\"gdb - %p\"  HelloWorld
java  -XX:OnError=\"gdb - %p\"  HelloWorld
The output now is:
java -XX:OnError="gdb - %p" HelloWorld
Unrecognized option: -
Could not create the Java virtual machine.
OK, the echo line outputs exactly what was previously executed directly from the shell, but the script's attempt to execute it fails. The error message hints that the
"gdb - %p"
is actually being interpreted as 3 different words for some strange reason, namely
"gdb
-
%p"
In fact, I think that I can confirm this by modifying the script to be
Code:
#!/bin/sh
echo java  -XX:OnError=\"gdb - %p\"  HelloWorld
set -x
java  -XX:OnError=\"gdb - %p\"  HelloWorld
set +x
which outputs
java -XX:OnError="gdb - %p" HelloWorld
+ java '-XX:OnError="gdb' - '%p"' HelloWorld
Unrecognized option: -
Could not create the Java virtual machine.
+ set +x
Notice how set -x puts the first pair of single quotes around just
-XX:OnError="gdb
and then the next pair around
%p"
So what is the shell script doing in its interpretation of the text that the shell command line does not do that is causing this to fail?

I guess that shell scripts are not exactly like saved command line sessions after all?

Could I work around this by using some sort of octal escape or something for the spaces inside "gdb - %p"?
  #2 (permalink)  
Old 12-18-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by fabulous2 View Post
So what is the shell script doing in its interpretation of the text that the shell command line does not do that is causing this to fail?
The shell is interpreting

Code:
java  -XX:OnError=\"gdb - %p\"  HelloWorld
as the following list of arguments....

0: java
1: -X:OnError="gdb
2: -
3: %p"
4: HelloWorld

The question is what do you actually want -XX:OnError to be?
  #3 (permalink)  
Old 12-18-2007
fabulous2 fabulous2 is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 30
Quote:
Originally Posted by porter View Post
The question is what do you actually want -XX:OnError to be?
Sorry if my inital psot was unclear. I want
-XX:OnError="gdb - %p"
to be interpreted as a single argument, not as 3 separate arguments as is currently happening.

I should have mentioned that I tried guessing and putting single and double quote marks around it, all to no avail. This is a very frustrating aspect of sh files compared to conventional languages like Java; there are so many gotchas that you feel like you are standing on quicksand sometimes.
  #4 (permalink)  
Old 12-18-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by fabulous2 View Post
I want
-XX:OnError="gdb - %p"
Try escaping the spaces that you want the shell to ignore....

Code:
-XX:OnError=\"gdb\ -\ %p\"
or quoting...

Code:
-XX:OnError="\"gdb - %p\""
  #5 (permalink)  
Old 12-19-2007
fabulous2 fabulous2 is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 30
Quote:
Originally Posted by porter View Post
Try escaping the spaces that you want the shell to ignore....

Code:
-XX:OnError=\"gdb\ -\ %p\"
or quoting...

Code:
-XX:OnError="\"gdb - %p\""
Does that really escape the spaces? I thought that the backslash only worked for certain subsequent chars (e.g. another \, or an n, etc).

Regardless, I tried it and it does not work.

I also tried using the octal code \040 instead of the space and the command now executes, but it uses the literal chars "\040" so that if a java error ever occured then it would fail at that point.

I had previously tried quotes like what you suggest, in both single and double quote versions, and both fail for me.
  #6 (permalink)  
Old 12-19-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
The \ escaping works for me on Solaris 9

C program compiled as a.out

Code:
#include <stdio.h>

int main(int argc,char **argv)
{
        int i=0;
        while (i < argc) { printf("argv[%d]=%s\n",i,argv[i]); i++; }

        return 0;
}
script...

Code:
#!/bin/sh

./a.out java "-XX:OnError=\"gdb - %p\"" HelloWorld
output

Code:
argv[0]=./a.out
argv[1]=java
argv[2]=-XX:OnError="gdb - %p"
argv[3]=HelloWorld
similarly...

Code:
#!/bin/sh -x

./a.out java -XX:OnError=\"gdb\ -\ %p\" HelloWorld
gives

Code:
argv[0]=./a.out
argv[1]=java
argv[2]=-XX:OnError="gdb - %p"
argv[3]=HelloWorld
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0