Ich würde normalerweise die Skripte mit dem Shebang definieren: #!/Bin/bash Im Allgemeinen ist #!/Bin/sh dasselbe wie #!/Bin/bash im Terminal , aber es kann einige Unterschiede in fortgeschrittenen Programmen geben . Speichern Sie das Skript als file.bash zum Beispiel Dann Sie das Skript ausführen, mit: $ bash file.bash
Und es läuft wie wäre es im Terminal. Wenn Sie mehrere Perl-Programme haben, können Sie alle NULL-Subroutinen (Funktionen) in einem großen Perl-Skript erstellen und dann die Unterprogramme nacheinander ausführen. Sehen Sie, wie ich 3 Scripte habe, die ich mit nur dieser Logik in Null-Subroutinen gemacht habe:
Unter dieser {# Ihr Code geht hier} # Angenommen, das ist Ihre Funktion.
Führen Sie Ihr Skript mit: "this()", und es tut was auch immer dies() tut.
Stellen Sie sich vor, dass diese grundlegenden Null-Subroutinen in diesem Perl-Skript, , separate Perl-Skripte sind. In einem Skript wie dieses ich wie so alle diese Perl-Skripte laufen ...
#!/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;
}
Mein Perl-Skript macht drei Dinge für die drei Unterprogramme. Er berechnet die Summe der Zahlen 1 bis n, wobei "n" vom Benutzer angegeben wird. Es ist viel zu freundlich, und es öffnet eine Datei und einfach gerade liest es Zeile für Zeile.
Zum Beispiel, hier ist, wie es funktioniert: Dies ist kein Code. Es ist Beispielausgabe vom Laufen des oben genannten Perlprogramms:
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!
Wie haben Sie mit '&&' versucht? Und welche Fehler hast du bekommen? –
Ich bin auf der Suche nach einer Möglichkeit, dies mit einem Bash-Skript zu implementieren, und nicht && – Marios
Bash führt Skriptbefehle nacheinander aus.So können Sie diese Befehle in eine Datei schreiben und ausführen. –