繁体   English   中英

在Linux的可执行脚本中结合多个perl脚本和命令

[英]Combine multiple perl scripts, commands in an executable script in linux

我创建了一些perl脚本,它们一个接一个地运行:

perl script_one.pl file.txt

perl script_two.pl

perl script_three.pl

命令file_one.log file_two.log

perl一线

例如

perl -lpe 's/\s*$//'

我如何将以上所有内容结合到bash脚本中?

我在Ubuntu 16.04机器上工作

我曾在Linux中尝试过&&,但出现错误

使用以下命令创建文件(例如script.sh ):

#!/bin/sh
perl script_one.pl file.txt
perl script_two.pl
perl script_three.pl

然后执行chmod +x script.sh./script.sh

我通常用shebang定义脚本:#!/ bin / bash通常,#!/ bin / sh与终端中的#!/ bin / bash相同,但是高级程序可能会有一些差异。 例如,将脚本另存为file.bash,然后使用以下命令执行脚本:$ bash file.bash

它的运行就像在终端上一样。

但是,如果您有多个perl程序,则可以在一个大的perl脚本中将它们全部设置为空子例程(函数),然后一个接一个地执行这些子例程。 看看我如何使用以下逻辑将3个脚本制作成空子例程:

sub this {#您的代码在此处}#假设这是您的功能。

使用“ this()”执行脚本,它执行this()所做的任何事情。

因此,请想象一下,此perl脚本中的这些基本null子例程是单独的perl脚本。 在这样的一个脚本中,我像这样运行所有的perl脚本...

 #!/usr/bin/env perl # multiple_programs.pl use strict; use warnings; use feature 'say'; # A subroutine can be a whole perl script. # Just write sub function { # all your code in a script }. # Then execute your subroutine with function() ... if your subroutine is called function, for example. # If you want to execute multiple scripts subroutines with no external data passed into them can be quite nifty! # If you define your variables within the subroutine and don't pass anything to them. # Notice how in this logical workflow I have defined no variables outside of the subroutines. # This can make your life easier sometimes. # Now we execute the null subroutines (functions) one after another like so. sum(); # Imagine this is the first script: sum.pl too_friendly(); # Imagine this is the second script: too_friendly.pl open_file(); # Imagine that this is the third script called open_file.pl sub sum { my $i = 1; print "Input the maximum value that you would like to sum numbers up to from 1: >>"; my $max = <STDIN>; my $sum; while($i <= $max) { $sum += $i; $i++; } print "The sum of The numbers 1 to $max is $sum\\n"; } sub too_friendly { # Put brackets around your whole code. say "\\nWhat's your name?"; my $name = <STDIN>; say "Hey buddy! Long time no see, $name!"; say "What's up!?"; } sub open_file { say "\\nI will open a file for you, and read the contents of it"; print "Okay, which file? >>"; chomp(my $file = <STDIN>); # The actual filename. my $filehandle; # A temporary variable to a file. unless(open($filehandle, '<', $file)){die "Could not open file $file";} while(my $rows = <$filehandle>) { print "$rows"; } close $filehandle; } 

我的perl脚本为这三个子例程完成了三件事。 它使用用户提供的“ n”来计算数字1到n的总和。 它太友好了,它会打开一个文件,然后直接向上逐行读取它。

例如,这是它的工作方式:这不是代码。 这是运行上述perl程序的示例输出:

 Input the maximum value that you would like to sum numbers up to from 1: >>15 The sum of The numbers 1 to 15 is 120 What's your name? Brother Hey buddy! Long time no see, Brother ! What's up!? I will open a file for you, and read the contents of it Okay, which file? >>A_file.txt Hey, What's happening? I am just file in your working directory, and you just opened me. Pretty cool, huh!? Okay, bye! 

暂无
暂无

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

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