免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4678 | 回复: 4
打印 上一主题 下一主题

如何用SYSTEM运行带"的命令? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-24 11:55 |只看该作者 |倒序浏览
我想在PERL中调用PHP的命令行模式发送邮件,很不幸地这个命令中即含有双引号,也含有单引号。命令如下:
/usr/bin/php -r 'mail("aaa@aaa.com","subject","contact");'
这一行命令在SHELL下直接执行是没有问题的。现在想在PERL中用system()来调用。但会报错。
#!/usr/bin/perl
system("/usr/bin/php -r 'mail("aaa@aaa.com","subject","contact");'");

出错如下:
Bareword found where operator expected at ./a.sh line 2, near ""/usr/bin/php -r 'mail("aaa"
        (Missing operator before aaa?)
Array found where operator expected at ./a.sh line 2, at end of line
String found where operator expected at ./a.sh line 2, near "com",""
Bareword found where operator expected at ./a.sh line 2, near "","subject"
        (Missing operator before subject?)
String found where operator expected at ./a.sh line 2, near "subject",""
Bareword found where operator expected at ./a.sh line 2, near "","contact"
        (Missing operator before contact?)
String found where operator expected at ./a.sh line 2, near "contact");'""
syntax error at ./a.sh line 2, near ""/usr/bin/php -r 'mail("aaa"
Execution of ./a.sh aborted due to compilation errors.

我估计有可能是命令中的引号引起这个问题,查了PERL手册也没找到解决办法,望大家出手相助,谢谢!

论坛徽章:
0
2 [报告]
发表于 2008-05-24 12:29 |只看该作者

回复 #1 lushy 的帖子

1.当你用""作为分界的时候,其中的"要backslash,照你的写法,perl会在这个地方帮你断句

  1. /usr/bin/php -r 'mail(
复制代码

2.“”和''是不同的,什么时候里面的变量会解释(interpolation),什么时候不会解释?
3.别忘了

  1. use strict;
  2. use warnings;
复制代码

@在perl中是有特殊含义的

论坛徽章:
0
3 [报告]
发表于 2008-05-24 12:36 |只看该作者
我引用的这条命令中即有双引号也有单引号,system()还可以用其它的符号引用命令么?

论坛徽章:
0
4 [报告]
发表于 2008-05-24 12:49 |只看该作者

回复 #3 lushy 的帖子

单引号和双引号已经足够了,只是你要注意用backslash转义,假如定界符是"的话如果其中还有"要\",这个在shell里面也是这么做的吧?
至于你说的其他引用符号有没有,是有的
Customary        Generic        Meaning        Interpolates
''        q//        Literal string        No
""        qq//        Literal string        Yes
``        qx//        Command execution        Yes
()        qw//        Word list        No
//        m//        Pattern match        Yes
s///        s///        Pattern substitution        Yes
y///        tr///        Character translation        No
""        qr//        Regular expression        Yes



see

  1. print qq ( "this is a test \n");
  2. print "this is a test\n";
复制代码

因为第一句的定界符是(),后一句的定界符是"",所以前一句的"不是特殊字符,运行的结果是有"的,后一句"的意义特殊
定界符可以随意,第一句也可以写成下面的形式,打印出的结果是一样的

  1. print qq @"this is a test\n"@;
  2. print qq {"this is a test\n"};
  3. print qq ["this is a test\n];
复制代码

你甚至可以这样

  1. print qq p"this is a test\n"p;
复制代码

下面的情况要注意转义,因为要打印的内容中含有定界符

  1. print qq e"this is a t\est\n"e;
复制代码

[ 本帖最后由 churchmice 于 2008-5-24 12:54 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-05-24 14:31 |只看该作者
多谢churchmice兄的详细讲解!在下也曾尝试过转义里面的双引号,看来是忘了将@也转义一下,我现在将双引号和@都转义后已经得到正确的运行结果了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP