Operating System - HP-UX
1833704 Members
2510 Online
110062 Solutions
New Discussion

executing a shell command within a awk command.

 
SOLVED
Go to solution
Prasad Joshi
Regular Advisor

executing a shell command within a awk command.

Hi All,

I am trying to execute a shell command from the awk script. But, It is not getting executed.

This is what I am trying =======>

sliam15:/ # cfsmntadm display sliam15 | awk '/mnt/ { print $1 }'
/mnt/sdg1/vol1
/mnt/sdg1/vol2
/mnt/sdg1/vol3
/mnt/sdg1/vol4
/mnt/sdg1/vol5
/mnt/sdg1/vset1

sliam15:/ # cfsmntadm display sliam15 | awk '/mnt/ { cfsmount $1 }'

Actually, it is not executing command "cfsmount" at all.

************************************************
I can execute the command mannually
sliam15:/ # cfsmount /mnt/sdg1/vol1
Mounting...
[/dev/vx/dsk/sdg1/vol1] mounted successfully at /mnt/sdg1/vol1 on sliam15
[/dev/vx/dsk/sdg1/vol1] mounted successfully at /mnt/sdg1/vol1 on sliam16
*****************************************************

How could I execute it in similar manner?

I know, I can use for loop and then cfsmount one by one. But, I want to know why the command I have given is not working?

Thanks in advance.

Thanks & regards,
Prasad.
5 REPLIES 5
Warren_9
Honored Contributor

Re: executing a shell command within a awk command.

hi,

how about using xargs?

# cfsmntadm display sliam15 | awk '/mnt/ { print $1 }' | xargs -n 1 cfsmount

GOOD LUCK!!
Prasad Joshi
Regular Advisor

Re: executing a shell command within a awk command.

Thanks Warren,
That was gr8.

I have one query related to xargs

I wanted to specify some options to the command given with xargs.

I tried this

sliam15:/ # cfsmntadm display sliam15 | awk '/mnt/ { print $1 }' | xargs -n1 echo $1 Test
Test /mnt/sdg1/vol1
Test /mnt/sdg1/vol2
Test /mnt/sdg1/vol3
Test /mnt/sdg1/vol4
Test /mnt/sdg1/vol5
Test /mnt/sdg1/vset1

Not getting why string "Test" is printed first and then the actuall mount point. It should have been printed the other way.

Thanks Again.
Prasad
Warren_9
Honored Contributor

Re: executing a shell command within a awk command.

hi,

i guess since $1 for the echo is "" (null string).

then the echo command will look like
"echo Test [the value pass from awk]"

GOOD LUCK!
Ninad_1
Honored Contributor
Solution

Re: executing a shell command within a awk command.

When you are using xargs , the output from the previous piped command will be treated as argument to what follows xargs.
Thus the actual execution of your echo will be like
echo $1 TEST /mnt/sdg1/vol1
echo $1 TEST /mnt/sdg1/vol2

and in this case $1 is null as it has not been set anywhere, hence the output.

Regards,
Ninad
Prasad Joshi
Regular Advisor

Re: executing a shell command within a awk command.

Thanks to all for help.

I found two ways to solve the problem

1. Use xargs.
cfsmntadm display sliam15 | awk '/mnt/ { print $1 }' | xargs -n 1 cfsmount

2. Use system()
cfsmntadm display slpam15 | awk '/mnt/ { system ( "cfsumount " $1 ) }'


Thanks & regards,
Prasad