Perl script that has command line arguments with spaces

I feel like I'm missing something pretty obvious here, but I can't figure out what's going on. I have a perl script that I am calling from C code. The script + arguments look something like this:

my_script "/some/file/path" "arg" "arg with spaces" "arg" "/some/other/file"

When I run it on Windows, Perl correctly identifies it as 5 arguments, whereas when I ran it on a SunOS Unix machine, it identified 8, breaking arg with spaces into separate arguments.

Not sure if this matters, but on Windows I run it as:

perl my_script <args>

While on Unix, I run it as an executable, for example, above.

Any idea why Unix is ​​mismanaging this argument?

Edit:

Here is the code to call the perl script:

 char cmd[1000];
 char *script = "my_script";
 char *argument = "\"arg1\" \"arg2\" \"arg3 with spaces\" \"arg4\" \"arg5\"";
 sprintf( cmd, "%s %s >1 /dev/null 2>&1", script, arguments);
 system( cmd );

This is not entirely true, as I am building the argument string a bit dynamically, but that is the point.

, :

($arg1, $arg2, $arg3, $arg4, $arg5) = @ARGV;

, , script C, .

+3
4

system("my_script \"/some/file/path\" \"arg\" \"arg with spaces\" \"arg\" \"/some/other/file\");

bash (- shebang, , ). , , , perl , , ( , perl , shebang).

Update:

:

char *argument = "\"arg1\" \"arg2\" \"arg3 with spaces\" \"arg4\" \"arg5\"";

:

char *argument = "\\\"arg1\\\" \\\"arg2\\\" \\\"arg3 with spaces\\\" \\\"arg4\\\" \\\"arg5\\\"";

:

, , .

argument GNU bash, version 4.0.28(2)-release (i686-pc-linux-gnu),

[sinan@kas src]$ ./t
'"arg1"'
'"arg2"'
'"arg3'
'with'
'spaces"'
'"arg4"'
'"arg5"'

argument . . , SUN bash , , - .

+3

SunOS?

perl script arg1 arg2 "arg3 with spaces" arg4 arg5

. .

+1

, , :

~% cat test.c
#include <stdio.h>
#include <stdlib.h>
int main(void){
        char cmd[1000];
        char *script = "./my_script";
        char *arguments = "\"arg1\" \"arg2\" \"arg3 with spaces\" \"arg4\" \"arg5\"";
        sprintf( cmd, "%s %s", script, arguments);
        system( cmd );
        return 0;
}

~% cat my_script
#!/usr/bin/perl
($arg1, $arg2, $arg3, $arg4, $arg5) = @ARGV;
print "arg1 = $arg1\n";
print "arg2 = $arg2\n";
print "arg3 = $arg3\n";
print "arg4 = $arg4\n";
print "arg5 = $arg5\n";

~% gcc test.c

~% ./a.out
arg1 = arg1
arg2 = arg2
arg3 = arg3 with spaces
arg4 = arg4
arg5 = arg5

~%

- .


:

Unix shells interpret quoted arguments as a single argument. You can do a quick test:

for i in a b "c d e" f; do echo $i; done

As a result, you expect this to be: "cd e" is treated as one argument.

I think you have a problem in your script processing logic arguments.

+1
source

Is it possible that the SunOS kernel passing the shebang interpreters is ridiculously bad? Try running it as " /path/to/perl script args" instead of " script args" and see if anything has changed.

0
source

Source: https://habr.com/ru/post/1664305/


All Articles