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

Help with a script 1

Status
Not open for further replies.

swortsoul

MIS
Jul 9, 2003
25
US
Hi all, I hope someone can help me with this. I've never been able to do this, and it would be very helpful now.

I need this script to give me the serial number of a known vpath. My problem, is the vpath I can grep for, is on one line, but the SERIAL #, is on the line below it. I need to grep for the vpath, then print the SERIAL # from the line below it. Can someone tell me how to do this?

Here's an example of the input I have (doesn't work well in this small window, the DEV# DEVICE NAME TYPE and POLICY are on one line, and SERIAL is on the line below it):

DEV#: 1 DEVICE NAME: vpath1 TYPE: 2105F20 POLICY: Optimized
SERIAL: 00120551
==========================================================================
Path# Adapter/Hard Disk State Mode Select Errors
0 fscsi0/hdisk4 CLOSE NORMAL 0 0
1 fscsi1/hdisk81 CLOSE NORMAL 0 0

DEV#: 2 DEVICE NAME: vpath2 TYPE: 2105F20 POLICY: Optimized
SERIAL: 00220551
==========================================================================
Path# Adapter/Hard Disk State Mode Select Errors
0 fscsi1/hdisk5 OPEN NORMAL 9572 0
1 fscsi0/hdisk111 OPEN NORMAL 9335 0

Any help would be appreciated.
Thanks
Tim
 
Here's a quick and dirty way to do it:

Code:
[i]command_that_produced_output[/i] |\
awk '( pathflag ){print $2;exit} /vpath1/{ pathflag=1 }'

will print out the serial number of vpath1.

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

 
Rod (or anyone else who can answer this),

I've got a script now that works. I don't quite understand what that statement is specifically doing. Also, since I want to use double quotes (to use a variable), it doesn't work the way it was intended.

This command:

datapath query device | awk '( pathflag ){print $2;exit} /vpath12/{ pathflag=1 }'

Returns: 00C20551

This one (using double quotes):

datapath query device | awk "( pathflag ){print $2;exit} /vpath12/{ pathflag=1 }"

Returns: SERIAL: 00C20551

I need the vpath12 from above to be $vpath_num (variable with current vpath#) which is why I tried double quotes. Right now, I can get around it using sed to just take the 2nd part.

So, do you know how I can do that?

Also, I've looked on the web and in the only awk refernce I have, and I don't know what pathflag does. Can you give a brief expanation of the syntax there?

Thanks
Tim
 
Tim,

I guess I should have said "quick, dirty, and obscure".

The main body of an awk program consists, basically, of a series of conditions and blocks of code to execute when those conditions are met. It then loops over the input file(s) a line at a time, evaluating the conditions in source order.

The script I posted makes use of implicit awk behaviour, which can lead to WORN (Write Once - Read Never) code. Here's a version that Steve McConnel (author or Code Complete, a must read for anyone doing any programming) would prefer. I'll also modify it to work with double quotes. Note: The BEGIN condition in awk is triggered before any lines are read, then never again. Comments begin with a # and run to the end of the line. I'll color them blue, in case the site wraps them.

Code:
VPATH_NUM=vpath12
datapath query device | awk "
BEGIN{
   pathflag=0; [COLOR=blue]# initialize flag[/color]
    }

( pathflag > 0 ){
[COLOR=blue]# pathflag has been increased, so we must have seen the vpath
# on the previous loop.[/color]

    print \$2; [COLOR=blue]# prints the second field (space delimited by default)
               # the dollar sign is escaped so the shell doesn't expand it
[/color]
    exit;      [COLOR=blue]# our work is done, stop all processing [/color]
    } [COLOR=blue]# end ( pathflag > 0 ) block[/color]

/$VPATH_NUM/{  [COLOR=blue]# forward slash enclosure indicates a pattern to match

               # the dollar sign in this case was not 
               # escaped, because we want it expanded
[/color]
   pathflag = 1; [COLOR=blue]# set flag to send message to next loop[/color]
} [COLOR=blue]# end vpath patter block[/color]
"

Working backwards to the original script, I took advantage two things:

1. logical operations actually return a numeric value for evaluation, zero for false and non-zero for true.
2. uninitialized variables evaluate as zero when referenced as a number.

So while pathflag is undefined, "( pathflag )" evaluates as false.

To make the one-liner work inside double quotes, just add a backslash before the "$2".

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top