![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
awk question
For the following code: Code:
ps auwx | awk -v "PID=${PID}" '$2 == PID {print $LN}'
Two points to clarify: 1. Is the -v flag for declaring we're going to work with a variable? 2. Does the $LN stands for current line? If so, what's wrong with $0? Any difference between them? Thanks |
|
||||
|
Quote:
Code:
ps auwx | awk -v "PID=${PPID}" '$2 == PID {print }'
xxxxxxx 598114 0.0 0.0 956 1048 - A 08:48:02 0:00 sshd: xxxxxxx@pts/1
prdrept:logs :ps auwx | awk -v "PID=$$" '$2 == PID {print }'
xxxxxxx 479452 0.0 0.0 752 800 pts/1 A 08:48:02 0:00 -ksh
this is using AIX, different systems could handle this differently. 1) LN does not show to be a defined variable in my awk. (I even checked the gawk manual to see if it were there.) Why use $0 when just a simple print will default to the present line. 2) I was not sure if you wanted the current process ($$) or the parent process ($PPID} - ${PID}, was not defined in my version of AIX. |
|
||||
|
Hi "awk", Thanks and sorry for not clarifying enough. The line is part of a running and working bash script on Linux. PID is a local variable and the awk line is using its value to print the relevant line from ps. I don't want to change the code (unless I have too), rather understand why it is used that way. to simplify it, it is something like: Code:
function foo()
{
local MY_PID=""
#...
MY_PID=get_my_pid
LINE=`ps auwx | awk -v "PID=${MY_PID}" '$2 == PID {print $LN}'`
do_something "${MY_PID}"
}
So, I understand the -v is used to declare variable, since ${MY_PID} will not be resolved inside 'single quotes' - right? I still don't understand what is the $LN for? Could anyone clarify? Is it a mistake, that works the same way as print $XXX works ??? Thanks |
![]() |
| Bookmarks |
| Tags |
| linux, solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|