![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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");
}
}
javac HelloWorld.java] Suppose that I execute the following command directly from the shell: java -XX:OnError="gdb - %p" HelloWorldThen 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 java -XX:OnError="gdb - %p" HelloWorldOK, 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 "gdbIn 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 java -XX:OnError="gdb - %p" HelloWorldNotice how set -x puts the first pair of single quotes around just -XX:OnError="gdband 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"? |
|
||||
|
Quote:
Code:
java -XX:OnError=\"gdb - %p\" HelloWorld 0: java 1: -X:OnError="gdb 2: - 3: %p" 4: HelloWorld 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. |
|
||||
|
Quote:
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. |
|
||||
|
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;
}
Code:
#!/bin/sh ./a.out java "-XX:OnError=\"gdb - %p\"" HelloWorld Code:
argv[0]=./a.out argv[1]=java argv[2]=-XX:OnError="gdb - %p" argv[3]=HelloWorld Code:
#!/bin/sh -x ./a.out java -XX:OnError=\"gdb\ -\ %p\" HelloWorld Code:
argv[0]=./a.out argv[1]=java argv[2]=-XX:OnError="gdb - %p" argv[3]=HelloWorld |
![]() |
| Bookmarks |
| Tags |
| linux |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|