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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More interesting q's.... 1

Status
Not open for further replies.

vptl

Technical User
Jan 22, 2004
120
US
Morning all,
Yes me again...still long way to go....I did tried to figure it out..but got no where...
Here is what I want to do.
I am doing in a while loop.
while ( $fs = <FS_LIST> ) {
chomp ($fs);
$line=`/nas/bin/fs_replicate -info $fs |egrep current_delta` ;
print "$fs.$line\n" ;
}
close (FS_LIST);


My command output is
<text1> = <Number1>
<text2> = <Number2>

for each $fs. Also for some $fs output is blank line.
I want to attach my $fs to each of the output line. Since I have two line output, $line is asssingmed only to entire string. and I get...
$fs.<text1> = <Number1>
<text2> = <Number2>

I am following O'Relly book (Learning Perl.) ,I saw example for %word which has only 2 clmns. in my case I have 3 clmns.

Thanks,

 
Here is what I am in looking into...
$line=system(qq!/nas/bin/fs_replicate -info $fs |egrep current_delta|sed s/\$/$fs/) ;
 
You probably want to assign the result of the backticks to an array instead of a scalar and then print that out.

Code:
my @lines = `/nas/bin/fs_replicate -info $fs |egrep current_delta`;
foreach my $line (@lines) {
  print "$fs.$line\n";
}

Try that and see what happens. :)


Trojan
 
Trojan,
Thanks...This works..if you do not mide can you please educate me...
1. "my" is it s std perl requirement to define array ?
b/c I saw example in the book which just says
@words = qw( x1,x2,x3) ;

2.when you use foreach my $lines (@lines)
is "my $lines" is the name of the array ?

3. @lines (in #2) is same is starting from 0,1,2..$ ?
like $line[0] ,$line[1]...and so on?

4. Please see why I cannot do this...
$line1=`/nas/bin/fs_replicate -info $fs |egrep current_delta|sed s/\$/_$fs/`;
if ( $line1 eq '' ) {
print "$fs IS NOT REPLICATING\n" ;
} else {
@info = qw("$line1") ;
print "$info[0]\n";
print "$info[1]\n";
print "$info[2]\n";
print "$info[3]\n";
}
#IMPmy @lines = `fs_replicate -info $fs |egrep 'current_delta|Error'`;
#IMPforeach my $lines (@lines) {
#IMPprint "$fs.$lines\n";
#IMP}
}
close (FS_LIST);

I get error in printing $info[0] and all of them...

I know so many questions..again....I am going to do this...until I know perl.! Thank you very much for you kind support.
 
This is the error I get....FS_LIST is my FileHndl
Use of uninitialized value in concatenation (.) at /tmp/nas_mon line 28, <FS_LIST> line 2.
 
Crikey!
What a lot of questions :-S

1. "my" is a means of "scoping" a variable. It causes the variable scope to be limited (in simple terms at least) to the surrounding curly braces ("{" and "}").

2. the "my $line" creates a pseudo reference to each element of the array "@lines" in turn.

3. Nearly, watch the "s". It walks through the array "@lines" as you suggest but each element of the array is "$lines[0]" and "$lines[1]" etc, not "$line[0]".

4. Sorry, not sure what you're trying to do here and I don't have time at the moment to look into it. I might get a moment later if no-one returns to you in the mean while.

Trojan

 
thanks.
just to let uou know what I am trying to do is assign each element of the output to a array variable so that I can test them as I need.
so like I mentioned earlier...my o/p is
<text1> = <Number1>
so array[1]=<text1>
array[2]="="
array[3]=<Number1>
while I am writing this..I think my second elect "=" is causing prob..I will chk.
No rush, when you have time...I know for you it will be...less then 1 min....
 
my $sample_string = "<text1> = <Number1>";
my @parts = split /\s/, $sample_string, -1;



Trojan.

 
I have my $line variable as

"current_delta_set 4410_fs1
current_delta_set 4409_fs1"

What I want is I want
info[1]=current_delta_set
info[2]=4410_fs1
info[3]=current_delta_set
info[4]=4409_fs1

 
Bingo !
Thank you very much sir...I think I am almost there...
Great stuff....Your PERL knowledge is very impressive.
BTW...(I cannot let you go w/o asking anything...lastone for today..promiss.) What is -1 for ?

 
This is my final script....
Still one problem...(for tomorrow.)
my while loop is not working...first I get $fs=fs1 and it stays there..!

Only and when you have some time....

#!/usr/bin/perl -w
system(q!/nas/bin/nas_fs -list | egrep -v 'root|name' | awk '{print $6}'|sed '/^$/d' > /tmp/fs_list!);
open (FS_LIST,"fs_list") ;
while ($fs=<FS_LIST>) {
chomp ($fs);
print "$fs\n";
$line=`/nas/bin/fs_replicate -info $fs |egrep current_delta|awk -F"=" '{print \$1,\$2}'|sed s/\$/_$fs/`;
chomp($line1);
if ( $line eq '' ) {
print "$fs IS NOT REPLICATING\n" ;
} else {
push @info, split(/\s+/, $line, -1);
print "$info[1]\n";
print "$info[3]\n";
}
print "$fs\n";
}
close (FS_LIST);

Thanks.
 
Got it.....the magic teaching.....
qq to interpret $fs..... :)
Will always remember you sir.
 
Now you're starting to really learn.
Enough to be able to work stuff out for yourself. It gets better and easier from here on.
BTW: I still think that you need to consider replacing your "system" functions and getting perl to do all the work.
I don't know what "/nas/bin/nas_fs" does and I guess you may have to keep that but certainly you could replace any greps awks and seds with simple perl regex's.

Trojan.

 
As Trojan says, shelling out to sed and awk may be convenient, but future maintainers of your script need to know three languages instead of one.

Suggested design: Lose the temporary files altogether.
Code:
use strict;
use warnings;

my @fslist = qx[/nas/bin/fs_replicate -info];
chomp @fslist;

foreach my $fs (@fslist) {
   next unless $fs =~ /root|name/;
   ...
}
Also, look up the obscure options and switches for the fs_* commands in the manual. Many command-line utilities have options to suppress headers and blank lines in their output to make scripting easier. It might save you a lot of hassle parsing the output, when you don't need to.
 
Trojan thanks for your kind words......And yes...you are my teacher....

Thanks to Steve also for his input in this regard.
I know all of you are doing this great work along with full time job.This is a great help for so many of my like,which is really beyond something normal people do.

What is qx ? what is "use strict " and "use warnings" do in the script....please do not mind I am still doing my first chapter....

Thanks.
V
 
qx" is similar to "qq" and "q". "q" allows you to define your own single quotes, "qq" double quotes and "qx" backticks.
When you use a character that is part of a pair (such as square or round brackets), the closing character would be the other of that pair (makes things look neater).

"use strict;" and "use warnings;" should really always be used to protect yourself from silly mistakes.
"use strict;" requires that you pre-delare all variables with "my" before you use them (or that you fully declare the package for a global variable wherever you use it). That might sound like a pain but it stop most typo errors.
You could also try "use diagnostics;" which is very useful for a beginner. When in force and an error occurs, it tries to explain in great detail what might have gone wrong and how to fix it.

What more could you ask for?!!! ;-)

Trojan.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top