简体   繁体   English

Perl Exec/System 传递参数

[英]Perl Exec/System passing parameter

I have an issue with the perl (v5.26.3) commands system or exec (both have the same behaviour)我对 perl (v5.26.3) 命令系统或 exec 有问题(两者具有相同的行为)

Both command work fine两个命令都可以正常工作

system ('git','pull','-ff','--no-rebase');
system ('git submodule --quiet foreach --recursive "echo \${name}"');

But when I split the "git submodule" into the arguments:但是,当我将“git 子模块”拆分为 arguments 时:

system ('git','submodule','--quiet foreach','--recursive "echo \${name}"');

Perl returns: Perl 返回:

usage: git submodule [--quiet] [--cached]
   or: git submodule [--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
   or: git submodule [--quiet] status [--cached] [--recursive] [--] [<path>...]
   or: git submodule [--quiet] init [--] [<path>...]
   or: git submodule [--quiet] deinit [-f|--force] (--all| [--] <path>...)
   or: git submodule [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--[no-]single-branch] [--] [<path>...]
   or: git submodule [--quiet] set-branch (--default|--branch <branch>) [--] <path>
   or: git submodule [--quiet] set-url [--] <path> <newurl>
   or: git submodule [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
   or: git submodule [--quiet] foreach [--recursive] <command>
   or: git submodule [--quiet] sync [--recursive] [--] [<path>...]
   or: git submodule [--quiet] absorbgitdirs [--] [<path>...]

How can I pass the arguments?如何通过 arguments?

For the system invocation that takes a list we need to pass it the command broken into words对于需要一个列表的系统调用,我们需要将分解成单词的命令传递给它

If there is more than one argument in LIST, or if LIST is an array with more than one value, starts the program given by the first element of the list with arguments given by the rest of the list .如果 LIST 中有多个参数,或者如果 LIST 是具有多个值的数组,则使用列表的rest 给出的 arguments 启动列表的第一个元素给出的程序。 If there is only one scalar argument, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing (this is /bin/sh -c on Unix platforms, but varies on other platforms). If there is only one scalar argument, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing (this is /bin/sh -c on Unix platforms, but varies on其他平台)。 If there are no shell metacharacters in the argument, it is split into words and passed directly to execvp , ...如果参数中没有 shell 元字符,则将其拆分为单词并直接传递给execvp ,...

(my emphasis) (我的重点)

Let's look at a trivial example.让我们看一个简单的例子。 Take a command like采取类似的命令

ls -l --sort size dir "dir A" 

There is no command ls -l but there is ls and its argument -l .没有命令ls -l但有ls及其参数-l There is also no argument --sort size ;也没有参数--sort size there is --sort , and (its value) size .--sort和(它的值) size But stuff protected by quotes, like "dir A" , need be passed as such.但是受引号保护的东西,例如"dir A" ,需要这样传递。 So, ('ls', '-l', '--sort', 'size', 'dir', '"dir A"')所以, ('ls', '-l', '--sort', 'size', 'dir', '"dir A"')

Same with '--quiet foreach' -- what argument is that , says git ?'--quiet foreach'相同git,这是什么论点?

So, not knowing that git command, I'd go with所以,不知道git命令,我会用 go

system('git', 'submodule', '--quiet', 'foreach', '--recursive', '"echo \${name}"');

See man 3 exec . man 3 exec

But it is also instructive, and perhaps more easily understood, to see how the shell (bash) does this, once it parses a line given to it.但是,一旦 shell (bash)解析给它的一行,它也是有启发性的,也许更容易理解。 It is the "Step 6" on the linked wiki page, where the command is finally prepared to be passed to the program.它是链接的 wiki 页面上的“第 6 步”,最后准备将命令传递给程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM