cancel
Showing results for 
Search instead for 
Did you mean: 

Variable syntax

 
SOLVED
Go to solution
Highlighted
Honored Contributor

Variable syntax


I am having trouble figuring out to reference two variables together

for example

grep $VGNAME/$LVNAME /etc/fstab |awk '{print$2}'

returns /vg01/lvol1
/vg01/lvol11
/vg01/lvol12

How do I bracket the $VGNAME/$LVNAME to get the exact match?

Thanks
Dave
3 REPLIES 3
Highlighted
Acclaimed Contributor
Solution

Re: Variable syntax

Hi Dave:

Enclose the expression in double quotes. Note the trailing space after ${LVNAME}.

It is also best to use curly braces around variable names.

# grep "${VGNAME}/${LVNAME} " /etc/fstab | awk '{print $2}'

Regards!

...JRF...
Highlighted
Acclaimed Contributor

Re: Variable syntax

Hi Dave:

If you prefer, one way in perl could be:

# perl -le '$str=shift;$FH=shift;open FH;while () {@fields=split;print $fields[1] if /$str\s+/'} ${VGNAME}/${LVNAME} /etc/fstab

...where you pass the argument you seek to match followed by the filename you want to examine -- /etc/fstab or perhaps /etc/mnttab.

Since perl fields are 0-relative, whereas 'awk' numbers from one (1) the perl snippet above references the same field that your 'awk' does.

Regards!

...JRF...
Highlighted
Honored Contributor

Re: Variable syntax

Thanks!

Dave