Operating System - HP-UX
1834919 Members
2950 Online
110071 Solutions
New Discussion

Re: OK...I need some shell script help..

 
SOLVED
Go to solution
Rita C Workman
Honored Contributor

OK...I need some shell script help..

I am down to the last of this thing...and I have lost it.
Take a look at the attached file that is giving me a headache. This is what I've created for doing disk mapping on multiple systems. I did it on 4 servers attached to our EMC disk array..
Each record can have between 3-5 fields (depending on if it's attached to lvol/filesystem or not)

What I am trying to do is get the alternate link (second line of same value as $1) to also print what is reported in the primary disk/lvol/file_sys line.
AND then I want to print a fairly clean report with a space in between different values of $1. Nothing fancy.

This is that disk mapping program I have been working on....and I'm down to this...
..ps..can you tell I am NOT a programmer ! Everytime I try to printf...it blows up because the first line does not have a value for $4 or $5 ... arghghgh !! I've been doing this in Posix Shell. My code is sloppy, but it got me to here..any Help would be appreciated !!

$1 = EMC info
$2 = hostname
$3 = disk addr info
$4 = /dev/vg/lvol
$5 = file_system

Thanks,
Rita
11 REPLIES 11
Vikas Khator
Honored Contributor

Re: OK...I need some shell script help..

Hi ,

How about sorting your file on first & fourth field and then applying your logic.

Keep it simple
Fred Myers
Advisor

Re: OK...I need some shell script help..

Actually EMC has software called I believe ECC that will do exactaly that and let you save it as a flat file, you can even import it into excell as a CSV and sort it if you want. Or you can use the HP utility inq.HP that is a free EMC HP binary awk out each servers information sort it compare it to /etc/lvmtab to get lvol info and the append it to a file on a leveraged machine with a `hostname` placed between them.
RikTytgat
Honored Contributor

Re: OK...I need some shell script help..

Rita,

I'd love to help you, but it would be easier if you submit your script!

Bye,
Rik.
James A. Donovan
Honored Contributor

Re: OK...I need some shell script help..

If you're just worried about the values of $4 and $5 being null, why not just test them before calling your printf statement? If they are null, then set them to some arbitrary value like "-" or "N/A".
Remember, wherever you go, there you are...
Tom Danzig
Honored Contributor

Re: OK...I need some shell script help..

I'm not sure I get exactly what you want, but you can try this:

awk '{if(NF==5){printf("\n%25s %10s - %s, Alt: %s\n",$4,$5,prev3,$3)}prev1=$1;prev3=$3}' name_of_your_file

Maybe you could post what you want the output to say and look like.
Bill Hassell
Honored Contributor
Solution

Re: OK...I need some shell script help..

Here's a POSIX/ksh script that will filter stdin into 5 parameters with missing parameters changed to " "

---

#!/usr/bin/sh
# Usage: cat somefile | parm5
# Output is always 5 values

# To remove the leading : use this:
# echo ${SN#:} ${HN#:} $DEV ${LVOL:-" "} ${MNT:-" "}

while read SN HN DEV LVOL MNT
do
echo $SN $HN $DEV ${LVOL:-" "} ${MNT:-" "}
done

---

The key is using shell parameter expansion. If LVOL or MNT are null or undefined, the result will be " " (or whatever you'd like). To use this script:

cat InputFile | this_script

Output will always be 5 values. Or you can pull the snippet into your script.


Bill Hassell, sysadmin
RikTytgat
Honored Contributor

Re: OK...I need some shell script help..

Hi,

What I would do:

while read line
do
set -- $line "" "" "" "" ""
done < your_file

That way, $1 to $5 always get filled up, even when your line only contains 3 (or less) fields (words). The 4th and 5th word will be empty, but you will not get an error (be sure to quote your variables when doing a prinf!!)

Hope this helps,
Rik.
Carlos Fernandez Riera
Honored Contributor

Re: OK...I need some shell script help..

Danny Engelbarts
Frequent Advisor

Re: OK...I need some shell script help..

Rita,

a very simple printf statement in the posix shell;

printf "%-x.xs%-x.xs%-x.xs%-x.xs%-x.xs\n" "$1" "$2" "$3" " $4" " $5"

where -x.x is used for presentation ;o)

by adding the space in front of $4 and $5 the argument to printf is always filled.

Greetz, Danny.
Tommy Palo
Trusted Contributor

Re: OK...I need some shell script help..

One more variant (rita.txt is your attachement):

#!/usr/bin/ksh
EMCP=" "
DADDRP=" "
LVOLP=" "
FSYSP=" "
sort -k1,1 -kr4,4 rita.txt -o rita.txt
while read EMC HOST DADDR LVOL FSYS
do
if [ "$EMC" != "$EMCP" ]; then
echo
fi
if [ -z "$LVOL" ]; then
LVOL=$LVOLP
fi
if [ -z "$FSYS" ]; then
FSYS=$FSYSP
fi
echo $EMC $HOST $DADDR $LVOL $FSYS | awk '{printf("%-12s %-12s %-24s %-24s %-24s\n",$1,$2,$3,$4,$5)}'
EMCP=$EMC
DADDRP=$DADDR
LVOLP=$LVOL
FSYSP=$FSYS
done < rita.txt
Keep it simple
Rita C Workman
Honored Contributor

Re: OK...I need some shell script help..

Many thanks to Mr. Hassell, I used your syntax pretty much verbatim....

Fred - Yes, I have a utility written by someone at EMC (Halstead..) I had posted it here once when I started on this thing. But it only lists using lvmtab, and we wanted to have the report reflect vg/lvol and file system.

Danny, your syntax looks alot like what I was trying to do...but needed Bill's above first.

Anyway, I got the report to print, although it isn't 'exactly' the way I want it...it's good enough and if I want to dress it up..I'll probably save my sanity and just dress it up in a spreadsheet on my PC.....
Again many many thanks to everyone for their input !

/rcw