#!/usr/bin/perl use strict; use warnings; my $g=main->new; $g->init; $g->parse; $g->enact; package main; sub new { return bless {}, shift; } sub init { my $this = shift; $this->{host1} = '.com'; $this->{host2} = '.com'; } sub parse { my $this = shift; my $cmd = join(' ', @ARGV); my ($host); if ($cmd =~ s/\-h\s*(\S*)//) { $host = $1; unless ($host eq $this->{host1}) { $this->{host1} = $host; $this->{host2} = ''; } } if ($cmd =~ s/\-c\s*(.*)$//) { $this->{sql} = $1; } else { $this->{sql} = ''; } $this->{cmd1} = '/bin/psql -h ' . $this->{host1} . ' ' . $cmd;; $this->{host2} and $this->{cmd2} = '/bin/psql -h ' . $this->{host2} . ' ' . $cmd; } sub enact { my $this = shift; if ($this->{sql}) { my $pid = open my $ph, "| $this->{cmd1}" or die $!; print $ph $this->{sql} . "\n"; close $ph; waitpid $pid, 0; if ($this->{host2}) { my $pid = open my $ph, "| $this->{cmd2} 2>&1 >/dev/null" or die $!; print $ph $this->{sql} . "\n"; close $ph; waitpid $pid, 0; } } else { my @stdin = ; my $pid = open my $ph, "| $this->{cmd1}" or die $!; print $ph @stdin; close $ph; waitpid $pid, 0; if ($this->{host2}) { my $pid = open my $ph, "| $this->{cmd2} 2>&1 >/dev/null" or die $!; print $ph @stdin; close $ph; waitpid $pid, 0; } } } 1; __END__