2017-07-18 143 views
0

我设计了几个Perl脚本,这是我在其他后运行一个:将多个Perl脚本,命令在一个可执行脚本,在Linux

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 SCR IPT?

我的Ubuntu 16.04机器

我曾与在Linux中& &尝试工作,但我得到的错误

+1

你是怎么用'&&'尝试的?你得到了哪些错误? –

+0

我正在寻找一种方法来实现这个与bash脚本,而不是&& – Marios

+1

Bash执行脚本的命令一个接一个。所以你可以在文件中编写这个命令并运行它。 –

回答

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

0

我通常会用shebang定义脚本:#!/ bin/bash 通常情况下,#!/ bin/sh与终端 中的#!/ bin/bash是一样的东西,但高级程序可能存在一些差异。 将脚本保存为file.bash,例如 然后你执行这个脚本: $庆典file.bash

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

sub this {#Your code goes here}#假设这是你的函数。

使用“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的总和,用户提供“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! 
+0

制作这样的脚本的好处在于,您可以拥有一个执行3个Perl脚本工作的perl脚本,而不是制作3个单独的perl脚本以及一个bash脚本。 – xyz123