0

It's a KSH problems.

I have encounter some problems with variables and quotation marks today. Order doesn't matter, although i cannot read the scripts but only execute it.

SomeScripts.sh "First=Harvey Lo","Third=Rebecca Won","Second=laugh at"

Results:

Harvey Lo laugh at Rebecca Won

but If i try to put it in a ksh scripts file, it doesn't work:

#!/bin/ksh
SOMEVAR=\"First=Harvey Lo\",\"Third=Rebecca Won\",\"Second=laugh at\"
SomeScripts.sh ${SOMEVAR}

Results:

Error: No. of ARGS > 3

I even try the following, but still not work.

#!/bin/ksh
SOMEVAR=\"First=Harvey Lo\",\"Third=Rebecca Won\",\"Second=laugh at\"
SomeScripts.sh `echo ${SOMEVAR}`

Results:

Error: No. of ARGS > 3

can anyone help, all i know that there is some java inside the SomeScripts.sh, the actual function have more than 20 parameters, but i have simplified as shown.

The Original Help File:

SomeScripts.sh First=Harvey,Third=Rebecca,Second=laugh
Harvey laugh Rebecca

1 Answer 1

1

You need to realize that your first example has only one parameter, and the way it is quoted is only a detail. These are identical:

SomeScripts.sh "First=Harvey Lo","Third=Rebecca Won","Second=laugh at"

SomeScripts.sh "First=Harvey Lo,Third=Rebecca Won,Second=laugh at"

So the way to make it work is not to put \" inside the variable – instead you need to quote it while it is being set:

SOMEVAR="First=Harvey Lo","Third=Rebecca Won","Second=laugh at"

Again, this is equivalent to a simple

SOMEVAR="First=Harvey Lo,Third=Rebecca Won,Second=laugh at"

You also need to quote the variable when it is used, otherwise it will be subject to word-splitting after being expanded. (Word-splitting here means just splitting by spaces.) The same applies to ` ` substitution as well.

SomeScripts.sh "$SOMEVAR"

Try compiling this program and running it with differently quoted arguments – it will show you exactly what it receives as arguments.

#include <stdio.h>
int main(int argc, char *argv[]) {
    printf("argc = %d\n", argc);
    for (argc=0; argv[argc]; argc++)
        printf("argv[%d] = %s\n", argc, argv[argc]);
    return 0;
}
3
  • Thanks i will try, I read the way it is quote from some previous working examples. not sure why they make that.... Actually those are CN, O, and OU .... but then, i am not quite understand how the first 2 you said are equivalent... the first one seems to have so many - (") double quotes, can you further explain ?
    – user218473
    Oct 24, 2013 at 12:38
  • 1
    @AntonyLee: The double-quotes only do one thing: they prevent the shell from splitting the argument into words at each space, and they're always stripped away before the shell actually runs the script. So it doesn't matter how exactly the argument is quoted – it could be "First=Harvey Lo" or First="Harvey Lo" or even First=Harvey" "Lo and it would still mean the same. The quotes just tell the shell to NOT split it into separate First=Harvey and Lo words. Oct 24, 2013 at 12:44
  • Thank, so essentially "Hello" is the same as "HE""""""""llo" ??
    – user218473
    Oct 24, 2013 at 12:52

You must log in to answer this question.