2017-07-18 78 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

相關問題