Perl脚本,其中命令行参数带有空格 [英] Perl script that has command line arguments with spaces

查看:51
本文介绍了Perl脚本,其中命令行参数带有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我在这里似乎遗漏了一些明显的东西,但是我似乎无法弄清楚发生了什么.我有一个从C代码调用的perl脚本.脚本+参数是这样的:

  my_script"/some/file/path""arg"带有空格的arg""arg""/some/other/file" 

当我在Windows中运行它时,Perl正确地将其标识为5个参数,而当我在SunOS Unix计算机上运行它时,它标识了8个参数,将带有空格的arg分成单独的args.

不确定它是否有任何区别,但是在Windows中,我的运行方式如下:

  perl my_script< args> 

在Unix中,我只是将其作为可执行文件运行,如上图所示.

有人知道为什么Unix无法正确管理该参数吗?

这是调用perl脚本的代码:

  char cmd [1000];char * script ="my_script";char * argument ="\" arg1 \"\" arg2 \"\" arg3带空格\"\" arg4 \"\" arg5 \";sprintf(cmd,%s%s> 1//dev/null 2>& 1",脚本,参数);系统(cmd); 

并非如此,因为我动态地构建了参数字符串,但这就是要点.

此外,这是我的代码,用于读取以下内容中的参数:

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

我知道它太天真了,但是此脚本只能从C应用程序运行,因此不需要更复杂的 .

解决方案

大概

  system("my_script \"/some/file/path \"\" arg \"\"带有空格的arg \"\" arg \"\"/some/other/file \); 

使所有内容都经过重击(因为需要解释shebang行,吃掉您传递的引号).同样,据推测,可以通过直接调用 perl 而不是依赖 shell 来避免该问题(尽管如果路径上的 perl 是与shebang线上提供的内容不同).

更新:

给出您的信息:

  char *参数="\" arg1 \"\" arg2 \"\" arg3带空格\"\" arg4 \"\" arg5 \"; 

您可能想尝试:

  char *参数="\\\" arg1 \\\"\\\" arg2 \\\"\\\" arg3带空格\\\\"\\\" arg4 \\\"\\\"arg5 \\\"; 

另一个更新:

谢谢您接受我的回答,但是,我的整个理论可能是错误的.

我在 GNU bash 4.0.28(2)-release(i686-pc-linux-gnu)和最终通过

[sinan @ kas src] $ ./t'"arg1"''"arg2"''"arg3''和'空格"'"arg4"''"arg5"'

而原始的 argument 就像一个咒语一样起作用.我对此有些疑惑.也许SUN上的外壳不是 bash ,或者可能正在发生其他事情.

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

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

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

Not sure if it makes any difference, but in Windows I'm running it like:

perl my_script <args>

While in Unix I'm just running it as an executable like show above.

Any idea why Unix is not managing that argument properly?

Edit:

Here's the code for calling 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 );

That's not exactly it, as I build the argument string a little more dynamically, but that's the gist.

Also, here's my code for reading the arguments in:

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

I know it's ridiculously naive, but this script will only be run from the C application, so nothing more complex should be required.

解决方案

Presumably

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

causes everything to go through bash (because of the need to interpret the shebang line, eating up the quotes you pass). Again, presumably, the problem could be avoided by invoking perl directly rather than relying on the shell to find it (although this might be a problem if the perl on the path is different than the one provided on the shebang line).

Update:

Given your:

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

you might want to try:

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

Another update:

Thank you for accepting my answer, however, my whole theory might be wrong.

I tried the double-backwhacked version of argument above in GNU bash, version 4.0.28(2)-release (i686-pc-linux-gnu) and it ended up passing

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

whereas the original argument worked like a charm. I am a little puzzled by this. Maybe the shell on the SUN isn't bash or maybe there is something else going on.

这篇关于Perl脚本,其中命令行参数带有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆