LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2011, 02:23 PM   #1
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Can an awk script find its own name?


Hello

According the The GNU Awk User's Guide "the value of ARGV[0] (see Built-in Variables) varies depending upon your operating system. Some systems put ‘awk’ there, some put the full pathname of awk (such as /bin/awk), and some put the name of your script (‘advice’). Don't rely on the value of ARGV[0] to provide your script name".

So is there any way for an executing awk script find its own name? AFAIK it has no way to determine its own PID so it can't use the process table via ps ...

Best

Charles
 
Old 05-13-2011, 02:49 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Didn't know this myself, but was intrigued by the question, a search came up with this:
Code:
BEGIN { getline t < "/proc/self/cmdline";split(t,T,"\0")
print "My name is:",T[3] }
Test run:
Code:
gawk -f myfile 
My name is: myfile

mv myfile /tmp/
gawk -f /tmp/myfile 
My name is: /tmp/myfile
Hope this helps.

Last edited by druuna; 05-13-2011 at 02:50 PM. Reason: spelling
 
1 members found this post helpful.
Old 05-14-2011, 12:10 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Here is another option for you, although you can use your favourite way to use a pid to get the name:
Code:
BEGIN{ cmd = "ps -p "PROCINFO["pid"]" -o comm="; print | cmd }
The difference here is that the script being called in druuna's fashion will yield gawk whereas:
Code:
#!/usr/bin/awk -f

<blah>
And called as:
Code:
./script.awk
Will get you script.awk

Last edited by grail; 05-14-2011 at 12:13 AM.
 
1 members found this post helpful.
Old 05-14-2011, 02:41 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@grail,

Maybe I misunderstand what you are saying, but my posted solution works both ways:

- Code is in a separate file, called by (g)awk (see post #2 for example),

- Code is part of a (g)awk script.

Code:
$ cat foo.awk
#!/usr/bin/awk -f

BEGIN { getline t < "/proc/self/cmdline";split(t,T,"\0")
print "My name is:",T[3] }

$ ./foo.awk
My name is: ./foo.awk
 
Old 05-14-2011, 03:31 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Thanks both

Despite scouring the awk man page for an equivalent of bash' $$ I had not found PROCINFO["pid"] so that was useful to learn.

I could not get the ps solution to work because the COMM field was not wide enough for my script's name and attempts to widen the column were not effective (-o comm:<big number>, -w option). Using cmd instead of comm worked but gave a space-separated list which would break if there was a space in the pathname so I chose the /proc method (which will (?) break for NULs in the pathname but that's acceptable).

Here's the final solution, adapting drunna's method to my verbose style that puts clarity above conciseness:
Code:
    getline cmdline < "/proc/self/cmdline"
    split( cmdline, array, "\0" )
    My_name = substr( array[3], match( array[3], /\/[^\/]*$/ ) + 1 )
Best

Charles
 
Old 05-14-2011, 03:51 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Surely you're not suggesting drunna has provided a less than effusive answer .... ????

ROTFL ...
 
Old 05-14-2011, 04:07 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by syg00 View Post
Surely you're not suggesting drunna has provided a less than effusive answer .... ????

ROTFL ...
Well, my code has longer variable names, more lines and more spaces ...

EDIT: regards coding style do are we establishing a terseness to verbosity spectrum going from grail via drunna to me?

Last edited by catkin; 05-14-2011 at 04:09 AM.
 
Old 05-14-2011, 04:18 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Trust me to be on the bottom rung ... sheesh ... LOL
 
Old 05-14-2011, 11:11 AM   #9
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by catkin View Post
attempts to widen the column were not effective (-o comm:<big number>, -w option)
To widen ps output, you need to set the COLUMNS environment variable: env COLUMNS=4096 ps ...
 
Old 05-15-2011, 12:02 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Thanks Nominal Animal but it didn't work in this case
Code:
c@CW8:~$ ps -p1961 -f
UID        PID  PPID  C STIME TTY          TIME CMD
root      1961     1  0 10:13 ?        00:00:00 /usr/sbin/console-kit-daemon --no-daemon
c@CW8:~$ ps -p1961 -o comm=
console-kit-dae
c@CW8:~$ export COLUMNS=4096
c@CW8:~$ ps -p1961 -o comm=
console-kit-dae
 
Old 05-15-2011, 12:04 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by grail View Post
Trust me to be on the bottom rung ... sheesh ... LOL
OK, a verbose to terse spectrum going from me via drunna to grail
 
Old 05-15-2011, 01:33 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So top to bottom still puts me down there .. hehe
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find URL in Debian package index via awk/sed (=find a line, then search from there) maddes.b Linux - Software 11 06-28-2013 07:37 AM
Bash script is enterpreting $1, $2 values in awk script ... praveen_218 Programming 4 09-14-2009 03:38 PM
URGENT: Help for Find and Replace with AWK onacorpuscle Linux - Newbie 3 12-07-2007 11:22 AM
can't find awk D_O_Y_L_E Linux - Software 1 03-21-2006 12:42 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration