Operating System - OpenVMS
1753872 Members
7534 Online
108809 Solutions
New Discussion юеВ

awk, external command and quotes

 
SOLVED
Go to solution
labadie_1
Honored Contributor

awk, external command and quotes

I am struggling with awk and an external command.

I want to do, in a awk script, an external call to a Vms command

If I do
$ gawk :== $ sys$common:[syshlp.examples.tcpip.snmp]gawk

Then if I create a simple file a.awk
BEGIN{}
{ system(" wr sys$output f$time() ") }
Then the command
$ gawk/input=a.awk sys$input
followed by another RETURN
displays correctly the current time.

Now, if I want to issue a command with quotes, like
wr sys$output f$getjpi("20400416","PRCNAM")

I get various errors.

I have tried various tripling of the quotes, to no avail.

Thanks for any hint.
26 REPLIES 26
labadie_1
Honored Contributor

Re: awk, external command and quotes

Of course, putting the command in a file and doing in the awk @file
works, but does not solve my problem, as I will have a pid varying

:-)
John Gillings
Honored Contributor

Re: awk, external command and quotes

Gerard,

Have you tried using PIPE? Perhaps something like:

$ PIPE WRITE SYS$OUTPUT "something" | -
GAWK/input=a.awk SYS$PIPE:

I find this is often a simple way of generating variable input without writing a temporary file. If the input is complex, you can use a command procedure to avoid having multiple continuation lines in the PIPE command, like this:

$ self=F$PARSE(";",F$ENVIRONMENT("PROCEDURE"))
$ IF p1.EQS."INPUT" THEN GOTO Input
$ IF p1.EQS."OUTPUT" THEN GOTO Output
$ PIPE @'self' "INPUT" | @'self' "OUTPUT"
$ EXIT
$
$ Input:
$ ! Generate your input stream here
$ ! using WRITE SYS$OUTPUT
$ EXIT
$
$ Output:
$ ! Execute command to consume the output
$ EXIT

(in your case you probably don't need the output branch, use your AWK command instead).
A crucible of informative mistakes
labadie_1
Honored Contributor

Re: awk, external command and quotes

Thanks John for the general method.

I realize I have not correctly explained my problem.

I am still working on the procedure I posted in the thread on "monitor network traffic"
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1120946
(By the way I have fixed a bug)
It displays some interesting data, like

BG270 ,Stream , 705,49153, 127.0.0.1, 127.0.0.1, 280, 0, 15, 0
BG778 ,Stream , 705,49185, 127.0.0.1, 127.0.0.1, 18748, 0, 1853, 0
BG281 ,Stream ,49154, 705, 127.0.0.1, 127.0.0.1, 25704, 0, 1837, 0
BG30531,Stream ,57678, 1800, 193.248.1.155, 10.196.10.30, 62464, 0, 245, 0
BG260 ,Dgram , 161, 0, , , 200310, 0, 5372, 0
BG604 ,Dgram , 3181, 0, 0.0.0.0, , 364228, 0, 35016, 0
BG32505,Stream , 1190,32801, 193.248.1.156, 10.196.11.80, 556708, 0, 59625, 0
BG24149,Stream ,50031, 1501, 193.248.1.155, 10.196.138.225, 2365544, 0, 253419, 0
BG671 ,Stream , 1190,42865, 193.248.1.156, 10.196.138.225, 15110388, 0, 260926, 0
BG710 ,Stream ,49182, 1616, 193.248.1.155, 193.248.120.121, 23229596, 0, 2488443, 0
BG711 ,Stream ,49183, 1515, 193.248.1.155, 193.248.120.120, 195537460, 0, 20934757, 0
BG749 ,Stream , 1190,60648, 193.248.1.156, 193.248.120.121, 826776008, 0, 3051698, 0
BG1201 ,Stream , 1190,64562, 193.248.1.156, 193.248.120.120, 880459944, 0, 22705543, 0
Device | type | ploc | prem | host local | host remote | bytes rec | bytes sent | I/O rec | I/O sen |
____________________________________________________________________________________________________________________


And I thought it could be good to add the pid and processname owning the Bg device.

So it is while I am inside awk that I want to get some info from Vms, typically

f$getdvi("bg123","pid")
and
f$getjpi(f$getdvi("bg123","pid"),"prcnam")

labadie_1
Honored Contributor

Re: awk, external command and quotes

In fact, I think the problem is in the syntax: in the awk file, I must have
{ system(" vmscommand ")}
So as long as I have
$ sh system
$ sh time
$ write sys$output f$time()
$ wr sys$output 1

It works fine.

But when I want to issue a command such as
$ wr sys$output "abcdef"
or any other with the character "
the parser wrongly takes the first " of the vms command as the end of the vms command

In fact, if you put as vms command
write sys$output "abcdef"
or
write sys$output

you get the same result
_Expression

:-(
Steven Schweda
Honored Contributor
Solution

Re: awk, external command and quotes

You're not thinking UNIX.

alp $ type a.awk
BEGIN{}
{ system(" write sys$output f$time() ") }

alp $ gawk /inp = a.awk sys$input

28-MAY-2007 07:55:48.97


And:

alp $ type b.awk
BEGIN{}
{ system(" write sys$output f$edit( f$time(), "COLLAPSE") ") }

alp $ type b.awk
BEGIN{}
{ system(" write sys$output f$edit( f$time(), "COLLAPSE") ") }
alp $ gawk /inp = b.awk sys$input

%DCL-W-ARGREQ, missing argument - supply all required arguments


But:

alp $ type c.awk
BEGIN{}
{ system(" write sys$output f$edit( f$time(), \"COLLAPSE\") ") }

alp $ gawk /inp = c.awk sys$input

28-MAY-200707:56:54.23

Hein van den Heuvel
Honored Contributor

Re: awk, external command and quotes

Free advice...

If you are going to do those getjpi prcnam for several sessions, then you may want to just pre-read some "show system" and stick the results into an array:

---- awk script ----
BEGIN { while ("show system" | getline) {
getjpi_prcnam[$1] = substr($0,10,15)
}
}
---------------------

Now you can just use getjpi_prcnam[pid] and know you are not spawning the system to a crawl.

But was the original input not from "show dev bg"? So you could just pick up process name and pic fron shwo dev/bg/full:

-----------------
BEGIN { while ("show device bg/full" | getline) {
if (/^Device/ ) {split ($2,x,","); bg = x[1];}
if (/UIC/) { split ($0,x,"\""); prcnam[bg] = x[2] }
if (/Owner process ID/) { pid[bg] = $4; print bg, x[2], $4; bg="X" }
}

for (bg in prcnam) {
print bg, pid[bg], prcnam[bg];
}
}


Or in original order:
--------------------------------

BEGIN { while ("show device bg/full" | getline) {
if (/^Device/ ) {units++; split ($2,x,","); bg[units] = x[1];}
if (/UIC/) { split ($0,x,"\""); prcnam[units] = x[2] }
if (/Owner process ID/) { pid[units] = $4; getjpi_prcnam[$4]=x[2] }
}
while (i++ < units) {
print bg[i], pid[i], prcnam[i];
}
}


I suspect the PERL is more suitable for this job though.


Enjoy,
Hein.
labadie_1
Honored Contributor

Re: awk, external command and quotes

Steven

Thanks for the tip, I should have known better, as I had struggled with a Linux command on several other Linux nodes with rsh and echo $HOSTNAME
:-)

Hein
Thanks for the valuable info, but as I do
$ ana/sys
tcpip sh dev/fu bg

I do not have the pid and processname owning the bg edvice.

$ sh dev /fu bgxxx
gives it, but not the counters, remote and local port, remote and local host...

And
$ tcpip sh dev/fu bgxxx
gives data with a "bad format", not good for searching
Hein van den Heuvel
Honored Contributor

Re: awk, external command and quotes

>> $ ana/sys .... tcpip sh dev/fu bg
>> I do not have the pid and processname owning the bg edvice.

I only see a PID in that output if there is an active IRP.

So I'm suggesting that if you do the regular DCL $SHOW DEV BG/FULL early, in the BEGIN section of the awk, then you can establish a known mapping from BG device name to PID and/or PRCNAM, through a simple bunch of arrays.
Then as you process the ANAL/SYS output, you can latch on to the BG name and look it up in those arrays for pid and/or prcnam.

>>. $ tcpip sh dev/fu bgxxx
gives data with a "bad format", not good for searching

yeah, sort of. It creates a long (815) byte record with embedded CR-LF.

That's what breaks a simple $PIPE tcpip sh dev/fu bgxxx | TYPE/PAGE/

It's not all that hard to process and specificall PERL automatically breaks it down into lines.

If you stick the output into a file, and then $convert/fdl="record; format stream" then the output will show as individual lines as well.

fwiw,
Hein.
Sebastian Bazley
Regular Advisor

Re: awk, external command and quotes

You could surely just pass the pid into the script:

$ gawk -v pid=val -f script.awk