Operating System - HP-UX
1830060 Members
2560 Online
109998 Solutions
New Discussion

adding the output of a command to a variable

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

adding the output of a command to a variable

we run

# ipcs -a |grep tss

output is something like

q 163 Ox54bc7e19 -rw-rw--- tssdba tssdba…..

so to remove the process i run

ipcrm -q 163.

The issue I have is that when I run # ipcs -a |grep tss there are several resources to clear.

Attempted to use a for loop:

for i in `ipcs -a |grep tss |awk {print "-" $1" "$2}'`
do
echo $i
done

-q
163
-q
345
-m
367

etc etc etc.

How can I add the first two fields to read as 1 variable so echo produces -m 163

Many thanks
hello
5 REPLIES 5
Hein van den Heuvel
Honored Contributor
Solution

Re: adding the output of a command to a variable


Hmmm why not solve it all in one awk command?

test:

ipcs -a | awk '/tss/{print "-" $1 " " $2}'

For real:

ipcs -a | awk '/tss/{system("ipcrm -" $1 " " $2)}'

hth,
Hein


lawrenzo_1
Super Advisor

Re: adding the output of a command to a variable

ok thats great - ran the test and the output was what I was looking for however where you have mentioned system in the real commnand - is this part of awk command?

THANKS
hello
Hein van den Heuvel
Honored Contributor

Re: adding the output of a command to a variable

Indeed, 'system' is a standard awk function to execute a command.

Since you wanted to do those ipcrm commands, just construct the command line in awk and pass as argument to 'system'.

Hein.
Muthukumar_5
Honored Contributor

Re: adding the output of a command to a variable

You can also try with shell as,

ipcs -a | awk '/tss/ { print "ipcrm -" $1" "$2}' | ksh

hth.
Easy to suggest when don't know about the problem!
lawrenzo_1
Super Advisor

Re: adding the output of a command to a variable

great stuff - thanks all
hello