×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Help with foreach problem...

Help with foreach problem...

Help with foreach problem...

(OP)
I have 2 of these loops to make sure that there are entries in the /etc/hosts file, but they don't run!? any ideas?


##Get the sub host names, the top level host is also used, this makes the single machine version work, too.
my $stmt = "SELECT hostname FROM $mysqldbname.host";

my $sth = $sqlhandle->prepare($stmt);
push (@err, "Error 202 in import.pl, can't prepare $stmt\n") if $DBI::err;

$sth->execute;
push (@err, "Error 203 in import.pl, can't execute $stmt\n") if $DBI::err;

my @sub_hosts;
@sub_hosts = $sth->fetchrow_array();
push(@sub_hosts, $informix_host);

##Get the corresponding array for ip's.
my $stmt = "SELECT ip_addr FROM $mysqldbname.host";
my $sth = $sqlhandle->prepare($stmt);
push (@err, "Error 203.2 in import.pl, can't prepare $stmt\n") if $DBI::err;
$sth->execute;
push (@err, "Error 203.4 in import.pl, can't exec $stmt\n") if $DBI::err;

my @sub_hosts_ip;
@sub_hosts_ip = $sth->fetchrow_array();
#print @sub_hosts;
###########################################################################################
#Make sure each sub_host is in the /opt/informix/etc/sqlhosts, and /etc/hosts
#as well, we may need to add them.
###########################################################################################
open (OLD, "< /opt/informix/etc/sqlhosts");
open (NEW, "> /opt/informix/etc/oldhosts");
$flag = 1;
select(NEW);

foreach $hst (@sub_hosts){
    print "HOST $hst";
while((<OLD>) && ($flag == 1)){
    @a = split(/#/,$_);
                   print STDOUT $a[0];
    if(/$hst/, $a[0]){
$flag = 0;
            print NEW $_;
    }else{
            print NEW $_;
    }
             }
if($flag == 0){
    #append new entry
    @these = split(/./,$hst);
    print NEW "ol_$these[0]    sesoctcp     $hst     sqlexec";
    $flag = 1;
}}

close(OLD);
close(NEW);
rename("/opt/informix/etc/sqlhosts", "/opt/informix/etc/prevhosts");
rename("/opt/informix/etc/oldhosts", "/opt/informix/etc/sqlhosts");
#######################################################################################
#Done adding to /opt/informix/etc/sqlhosts->add to /etc/hosts now!
######################################################################################

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close