Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Bash命令输出通过管道传输到Bash脚本中的多行Perl代码?_Perl_Bash_Pipe - Fatal编程技术网

如何将Bash命令输出通过管道传输到Bash脚本中的多行Perl代码?

如何将Bash命令输出通过管道传输到Bash脚本中的多行Perl代码?,perl,bash,pipe,Perl,Bash,Pipe,对于Bash脚本中的以下管道: Bash command | perl -ne 'single line perl command' | Another Bash command Perl命令只能是单行。如果我想编写更复杂的多行Perl命令,如何。我是否可以为每个perl命令行使用多个“-e”选项,例如: perl -n -e 'command line 1' -e 'command line 2' -e 'command line 3' 或者我可以为多行Perl代码使用“Here Docu

对于Bash脚本中的以下管道:

Bash command | perl -ne 'single line perl command' | Another Bash command
Perl命令只能是单行。如果我想编写更复杂的多行Perl命令,如何。我是否可以为每个perl命令行使用多个“-e”选项,例如:

perl -n -e 'command line 1' -e 'command line 2' -e 'command line 3'
或者我可以为多行Perl代码使用“Here Document”(在这种情况下,Perl选项,例如“-n”,应该仍然可以指定)

如果可以使用“heredocument”,谁能举例说明如何使用它


提前感谢您的建议。

对于多行Perl,您只需要用分号分隔命令,而不是传递多个
-e
调用。例如:

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

(虽然我通常不会说Perl块本身就是一个“命令行”。

对于多行Perl,您只需要用分号分隔命令,而不是传递多个
-e
调用。例如:

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

(虽然我通常不会说Perl块本身就是一个“命令行”。

如果您的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
手册还说:

-e命令行

可用于输入一行程序。如果给定了
-e
,Perl将不会在参数列表中查找文件名。可以给出多个
-e
命令来构建多行脚本。确保在普通程序中使用分号

-E命令行

其行为与-e类似,只是它隐式启用了所有可选功能(在主编译单元中)

所以你也可以做:

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

如果脚本超过,比如说,20行(10到50行之间的某个位置,多少取决于选择),那么可能是时候将Perl脚本分离到它自己的文件中并运行它了。

如果您的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
手册还说:

-e命令行

可用于输入一行程序。如果给定了
-e
,Perl将不会在参数列表中查找文件名。可以给出多个
-e
命令来构建多行脚本。确保在普通程序中使用分号

-E命令行

其行为与-e类似,只是它隐式启用了所有可选功能(在主编译单元中)

所以你也可以做:

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
如果脚本超过,比如说,20行(10到50行之间的某个位置,多少取决于选择),那么可能是时候将Perl脚本分离到它自己的文件中并运行它了