简体   繁体   English

如何在Bash脚本中将Bash命令输出传递给多行Perl代码?

[英]How to pipe Bash command output to multi-line Perl code in a Bash script?

For the following piping in a Bash script : 对于Bash脚本中的以下管道:

Bash command | perl -ne 'single line perl command' | Another Bash command

The Perl command could only be single line. Perl命令只能是一行。 How about if I want to write more complex multi-line Perl commands. 如果我想编写更复杂的多行Perl命令,该怎么办。 Could I use multiple "-e" options for each perl command line, such as : 我可以为每个perl命令行使用多个“ -e”选项,例如:

perl -n -e 'command line 1' -e 'command line 2' -e 'command line 3'

Or could I use a "Here Document" for the multi-line Perl codes (the perl options, such as "-n", should still be able to be specified in such case.) ? 还是我可以对多行​​Perl代码使用“此处文档”(在这种情况下仍应可以指定perl选项,例如“ -n”。)?

If "Here Document" could be used, could anyone illustrate how to use it with an example. 如果可以使用“此处文档”,那么谁能举例说明如何使用它。

Thanks in advance for any suggestion. 预先感谢您的任何建议。

If your Perl script is simply too long to be controllable on a single line (with semi-colons separating Perl statements), then bash is quite happy to extend your single quoted argument across as many lines as you need: 如果您的Perl脚本太长而无法在一行上进行控制(用分号分隔Perl语句),那么bash很高兴将您的单引号参数扩展为所需的任意行:

Bash-command |
perl -ne 'print something_here;
          do { something_else } while (0);
          generally("avoid single quotes in your Perl!");
          say q{Here is single-quoted content in Perl};
         ' |
Another-Bash-Command

Also the 'perldoc perlrun' manual says: “ perldoc perlrun”手册也说:

-e commandline

may be used to enter one line of program. 可以用来输入一行程序。 If -e is given, Perl will not look for a filename in the argument list. 如果给定-e ,Perl将不在参数列表中查找文件名。 Multiple -e commands may be given to build up a multi-line script. 可以使用多个-e命令来构建多行脚本。 Make sure to use semicolons where you would in a normal program. 确保在普通程序中使用分号。

-E commandline

behaves just like -e, except that it implicitly enables all optional features (in the main compilation unit). 行为与-e相同,除了它隐式启用所有可选功能(在主编译单元中)。

So you could also do: 因此,您也可以这样做:

Bash-command |
perl -n -e 'print something_here;' \
        -e 'do { something_else } while (0);' \
        -e 'generally("avoid single quotes in your Perl!");' \
        -e 'say q{Here is single-quoted content in Perl};' |
Another-Bash-Command

If the script gets bigger than, say, 20 lines (somewhere between 10 and 50, somewhat according to choice), then it is probably time to separate the Perl script into its own file and run that instead. 如果脚本大于20行(介于10到50行之间,根据选择而有所不同),那么可能是时候将Perl脚本分离到自己的文件中并运行它了。

For multiple lines of Perl, you simply need to separate the commands with semicolons rather than pass multiple -e calls. 对于多行Perl,您只需要用分号分隔命令,而不是传递多个-e调用。 For example: 例如:

perl -n -e 'command line 1; command line 2;'

(though I wouldn't normally say a Perl block was a "command line", per se). (尽管我通常不会说Perl块本身就是一个“命令行”)。

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

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