Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I work line by line of an array and split line into fields?

Status
Not open for further replies.

tmf33uk

Technical User
Aug 23, 2007
21
GB
Hi,

I need help with a loop in a script that is not doing what I want it to do. I include the whole script at the bottom of this message. The intention is that (1) it reads a file /var/opt/oratab into an array (this works), (2) It should go line by line, and separating the fields by : so that the 2nd one can be assigned to a variable with which I can run an OS command (per line) to check the permissions of bin files within each of these directories.

As it is now, the script prints the @lines array and then it is checking the permissions of the binary files ending in zero in $ORACLE_HOME/bin of the first oratab line, /opt/oracle/product/db10g/bin/*0.

I am not clear as to whether the array has stored the lines from oratab into one line, or it is just the way it lists them. If it lists them all into one line consecutively, then I can see why

my $ORACLE_HOME=$line[1];

only reads the first directory and not one for each line in the oratab. How can I get it to work through the array, line by line? :eek:(

Thanks again!

Teresa.

#################################################
/var/opt/oratab contains:
repo:/opt/oracle/product/db10g:Y
agent:/opt/oracle/product/agent10g:Y
oms:/opt/oracle/product/oms10g:Y
emdiag:/opt/oracle/product/emdiag:Y
##################################################
prueba.pl:

#!/usr/bin/perl

use strict;

my @lines=();
my $line;
my @line=();
my $filename = "/var/opt/oratab";
my $bincero;
my @bincero=();
my $perms;
my $dev;
my $pattern;
my @bin;
my @fields=();

# print "Oratab: $filename\n";
#========================================================
#Read from oratab file
#========================================================
open (FILE, "< $filename\n") or die "Cant open $filename: $!\n";
while (<FILE>)
{
s/"*"//; #ignore comments and ORA_HOME with no db defined
next if /^(\s)*$/; #skip blank lines
chomp;
push (@lines, $_);
}
print @lines, "\n";
close FILE;
foreach $line (@lines)
{
@line= split (/:/, $line);
my $instance=$line[0];
my $ORACLE_HOME=$line[1];
my $linea;
my @bincero = `ls -la $ORACLE_HOME/bin/*0`;
my @binO = `ls -la $ORACLE_HOME/bin/*O`;
# my @allredunbins = (@bincero, @binO); #merges both arrays into one.
#print "Redundant binaries ending in O in $ORACLE_HOME, regardless of perms are as follows:\n @allredunbins\n";
foreach $linea(@bincero)
{
@fields = split(m/[\n\t\s]+/, $linea);
$perms = $fields[0];
$dev = $fields[8];
$pattern = "----------";
# {
if ($perms !~ $pattern) #Check for permissions above 000 in each binary.
{print "em_result: Binary $dev has excessive perms: $perms\n"; }
# }
}
exit 0;
}
##########################################################
 
Your logic is checking the first record in the file for comments/blanks. When it reaches the first nonblank line, it
chomps, appends to an array, and then CLOSES THE INPUT FILE. You then iterate through your array which can only have 1 element.

Proof of Concept:
Code:
[olive][b]while[/b][/olive][red]([/red]<DATA>[red])[/red] [red]{[/red]
  [red]s/[/red][purple]"*"[/purple][red]/[/red][purple][/purple][red]/[/red][red];[/red]        [gray][i]#ignore comments and ORA_HOME with no db defined[/i][/gray]
  [olive][b]next[/b][/olive] [olive][b]if[/b][/olive] [red]/[/red][purple]^([purple][b]\s[/b][/purple])*$[/purple][red]/[/red][red];[/red]    [gray][i]#skip blank lines[/i][/gray]
  [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
  [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [red]([/red] [blue]$instance[/blue], [blue]$ORACLE_HOME[/blue] [red])[/red] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url] [red]/[/red][purple]:[/purple][red]/[/red][red];[/red]
  [olive][b]foreach[/b][/olive] [red]([/red] [red]`[/red][purple]ls -la [blue]$ORACLE_HOME[/blue]/bin/*0[/purple][red]`[/red] [red])[/red] [red]{[/red]
		[black][b]my[/b][/black] [red]([/red][blue]$perms[/blue], [blue]$dev[/blue] [red])[/red] = [red]([/red][black][b]split[/b][/black] [red]/[/red][purple][[purple][b]\n[/b][/purple][purple][b]\t[/b][/purple][purple][b]\s[/b][/purple]]+[/purple][red]/[/red][red])[/red][red][[/red][fuchsia]0[/fuchsia],[fuchsia]8[/fuchsia][red]][/red][red];[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]em_result: Binary [blue]$dev[/blue] has excessive perms: [blue]$perms[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red] [olive][b]if[/b][/olive] [red]([/red][blue]$perms[/blue] ne [red]'[/red][purple]----------[/purple][red]'[/red][red])[/red][red];[/red]
	[red]}[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]repo:/opt/oracle/product/db10g:Y[/teal]
[teal]agent:/opt/oracle/product/agent10g:Y[/teal]
[teal]oms:/opt/oracle/product/oms10g:Y[/teal]
[teal]emdiag:/opt/oracle/product/emdiag:Y[/teal]

I also changed the permissions comparison to a simple string comparison.
 
This looks fine to me:
Code:
open (FILE, "< $filename\n") or die "Cant open $filename: $!\n";
while (<FILE>)
    {
    s/"*"//;        #ignore comments and ORA_HOME with no db defined
    next if /^(\s)*$/;    #skip blank lines
    chomp;
    push (@lines, $_);
    }

the above should process the entire file and push all lines that meet the rquirements into @lines. I think the poor indentation of the code mislead you brigmar.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Sorry, should have said, all 4 lines in the oratab file are appended to the array as it is. This is the output of the script as it is. As you´ll notice, the line
print @lines, "\n";
does print all 4 lines one after the other. This is, I think, why I can´t see the way to treat line by line (of the oratab). Does the array see all 4 lines as one record or is it just the way it prints it out?

[grid@raclinux1 scripts]$ perl ./prueba.pl
repo:/opt/oracle/product/db10g:Yagent:/opt/oracle/product/grid10g/agent10g:Yoms:/opt/oracle/product/grid10g/oms10g:Yemdiag:/opt/oracle/product/emdiag:Y
repo:/opt/oracle/product/db10g:Y
em_result: Binary /opt/oracle/product/db10g/bin/dumpsga0 has excessive perms: -rwxr-x---
em_result: Binary /opt/oracle/product/db10g/bin/e2eme0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/emagent0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/emagtm0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/emdctl0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/emtgtctl20 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/kfod0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/lsnrctl0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/mapsga0 has excessive perms: -rwxr-x---
em_result: Binary /opt/oracle/product/db10g/bin/nmb0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/nmccollector0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/nmei0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/nmo0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/nmocat0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/nmupm0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/nmus0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/ocrcheck0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/ocrconfig0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/ocrdump0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/oifcfg0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/okdstry0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/okinit0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/oklist0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/osdbagrp0 has excessive perms: -rwxr-x---
em_result: Binary /opt/oracle/product/db10g/bin/tnslsnr0 has excessive perms: -rwxr-xr-x
em_result: Binary /opt/oracle/product/db10g/bin/tnsping0 has excessive perms: -rwxr-xr-x
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top