Thread von shell-script nach perl (13 answers)
Opened by Gast at 2005-01-29 02:00

Ronnie
 2005-01-29 23:29
#51424 #51424
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
So, erstmal ein wenig perl:
Code (perl): (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/perl

use strict;
use warnings;

use CGI;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use HTML::Template;
use Tie::File;
#use Data::Dumper qw(Dumper);

my $q = new CGI;
my $file = shift @ARGV;
my $mark = shift @ARGV;

my $file_raw  = $file.".raw";
my $file_desc = $file.".desc";

#print $q->header(), Dumper(&process_files());
print $q->header(), &fill_template(&process_files());
exit;

sub process_files {
  my @file_raw;
  my @result_set;
  tie @file_raw, 'Tie::File', $file_raw or die "Can't tie to file $file_raw: $!\n";
  @result_set = map { 
    my (undef, $host, $ip ) = split(" ", $_);
    my $highlight = ($host =~ /$mark/g) ? 'mark' : undef;
    { host => $host, ip => $ip, highlight => $highlight, };
  } 
  @file_raw;
  return \@result_set;
}

sub fill_template {
  my $recordset = shift;
  my $template = HTML::Template->new(
    filename => "report.tmpl",
    die_on_bad_params => 0,
  );
  $template->param( title => 'Entwurf V2.0', recordset => $recordset, );
  return $template->output;
}

dann das Template:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<html>
<head>
<title><TMPL_VAR name="title"></title>
<style>
<!--
.mark { color: #f00; }
-->
</style>
</head>
<body>
<table>
<TMPL_LOOP name="recordset">
<tr class='<TMPL_VAR name="highlight">'>
<td><TMPL_VAR name="ip"></td>
<td><TMPL_VAR name="host"></td>
</tr>
</TMPL_LOOP>
</table>
</body>
</html>

dann meine test.raw:
Code: (dl )
1
2
x host1.subdomain.domain.xx 10.10.10.1
x host2.subdomain.domain.xx 10.10.10.2

Es erfüllt noch nicht komplett deine Anforderung, aber es zeigt wie es sauber in Perl gelöst werden kann. Wenn ich es noch etwas ausbauen soll sage mir Bescheid.

View full thread von shell-script nach perl