Thread Skript für automatische Backups (27 answers)
Opened by mollilinux at 2010-02-23 14:06

pq
 2010-02-23 14:20
#133517 #133517
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
du solltest mit strict und warnings arbeiten. warnings hätte dich wohl hier gewarnt, dass du die subroutine doConnect in der schleife wieder und wieder neu definierst.
bitte lesen: Wiki:use strict

ich habe mal den merkwürdigen teil verkürzt:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
sub exec_command {
    system("doConnect() $cmd_login $ssh_bin $startTime $duration $auth");
}

while (...) {
        sub doConnect {
              $cmd_login = "$user\@$host";
              $ssh_bin = `which ssh`; chomp $ssh_bin;
              exec_command();
        }
        doConnect();


du definierst eine sub jedesmal neu, und rufst dann direkt danach doConnect auf, welches globale variablen setzt und dann exec_command ohne parameter aufruft.

subroutinen sind dafür da, dass man ihnen parameter übergeben kann. und eine sub ständig neu zu definieren und gleich danach aufzurufen, da erschliesst sich mir der sinn nicht ganz.

so würde ich das machen:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
sub exec_command {
    my ($cmd_login, $ssh_bin, $startTime, $duration, $auth) = @_;
    system("doConnect() $cmd_login $ssh_bin $startTime $duration $auth");
}
while (...) {
    my $cmd_login = "$user\@$host";
    my ssh_bin = `which ssh`;
    chomp $ssh_bin;
    ...
    exec_command($cmd_login, $ssh_bin, $startTime, $duration, $auth);
}


das system("doConnect() ...") kommt mir etwas merkwürdig vor, aber das hab ich mal so gelassen, weil ich nicht weiss, welche shell du benutzt und was das bezwecken soll.
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem

View full thread Skript für automatische Backups