Schrift
[thread]8638[/thread]

system Aufruf mit Perl in Array speichern (Seite 2)

Leser: 1


<< |< 1 2 >| >> 13 Einträge, 2 Seiten
bloonix
 2007-01-12 13:56
#73145 #73145
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
[quote=krusty,12.01.2007, 12:42]Noch ne recht bescheidene Frage. Wie prüfst du die Variable ($?)?

Einfach nur ne einfache if Anweisung?[/quote]
Japp.

Zum Beispiel:
die "Ausfuehrung von fgrep ist fehlgeschlagen: $?" if $?;

und mit CPAN:IPC::Open3 kann man recht netten Code schreiben.

Und um es ein wenig zu übertreiben... würde ich noch den Schalter -T
benutzen. ;)\n\n

<!--EDIT|opi|1168615768-->
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.
bloonix
 2007-01-12 17:59
#73146 #73146
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Hier mal ein simples Beispiel mit CPAN:IPC::Open3

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/perl -wT
use strict;
use IPC::Open3;

$ENV{PATH} = '/sbin';

my ($wtr, $rdr, $err, $ret);
my $pid = open3($wtr, $rdr, $err, 'ifconfig', '-abc');
close $wtr;
waitpid $pid, 0;
$ret = $? / 256;

if ($ret == 0) {
  while (my $line = <$rdr>) {
     print $line; # oder deine Verarbeitung
  }
} else {
  local $/;
  my $errors = $err ? <$err> : <$rdr>;
  print STDERR "Unable to execute ifconfig: ($ret)\n";
  print STDERR "$errors" if $errors;
}

close $rdr;
close $err if $err;

exit $ret;


#> ./test.pl
Unable to execute ifconfig: (1)
ifconfig: option `-abc' not recognised.
ifconfig: `--help' gives usage information.
\n\n

<!--EDIT|opi|1168618864-->
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.
havi
 2007-01-12 23:05
#73147 #73147
User since
2003-08-04
2036 Artikel
BenutzerIn
[Homepage]
user image
[quote=opi,12.01.2007, 10:39]
Code: (dl )
1
2
my @test = qx'fgrep "(22.5.1983)" /test/old/* -l';
print "Debug: ",@test,"\n";


Wenn du den Schalter -w benutzt, dann brauchst du kein "use warnings;".[/quote]
Besser
Code: (dl )
use warnings;

verwenden.
<< |< 1 2 >| >> 13 Einträge, 2 Seiten



View all threads created 2007-01-12 10:59.