我通常會用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!
你是怎麼用'&&'嘗試的?你得到了哪些錯誤? –
我正在尋找一種方法來實現這個與bash腳本,而不是&& – Marios
Bash執行腳本的命令一個接一個。所以你可以在文件中編寫這個命令並運行它。 –