在linux中的可执行脚本中组合多个perl脚本,命令

时间:2017-07-18 09:55:34

标签: linux bash perl

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

perl script_one.pl file.txt

perl script_two.pl

perl script_three.pl

命令file_one.log file_two.log

perl one liner

例如

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

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

我在ubuntu 16.04机器上工作

我试过过&&在linux中,但我有错误

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

它就像在终端中一样运行。

但是如果你有多个perl程序,你可以在一个大的perl脚本中使它们全部为空子程序(函数),然后一个接一个地执行子程序。看看我有3个脚本,我只用这个逻辑做了null子程序:

sub 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脚本为三个子例程做了三件事。 它计算数字1到n的总和,用&#34; n&#34;由用户提供。 它太友好了,它会打开一个文件,然后直接逐行读取它。

例如,以下是它的工作原理: 这不是代码。它是运行上述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!