Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

correctly starting awk scripts
Hi,

I wanted to process options in my awk scripts starting by:

#!/usr/bin/awk -f
BEGIN {
for (i = 1; i < ARGC; i++) print ARGV[i]
exit
}

I found that it was impossible to pass arguments starting with "-",
as they were eaten by awk itself rather than my script.
And yes, I know how to use "--".

Here is now my solution:

#!/bin/sh
#
# The following code line is interpreted both by awk and /bin/sh.
# In sh, it's a command line that restarts awk to interpret this script.
# In awk, it's a combined pattern with a null action (no side effect).
# The rest of script is only interpreted by awk.
#
exec awk -f "$0" -v "script=$0" "--" "$@" {}
BEGIN {
ARGC--	# skip the command line null action
for (i = 1; i < ARGC; i++) print ARGV[i]
exit
}

It has the following advantages:
- The location of awk has not to be hardwired in the script.
- no limitation about the pathname size, nor the number of args
(limitided respectively to 30, 1 in previous usage).
- the name of command is correctly passed as a variable
- works even if awk is a shell script (i.e. to automatically include other
files...).
- of course, allow to pass arguments starting with "-".

Cheers.

--Marc

Report this thread to moderator Post Follow-up to this message
Old Post
Marc Vertes
01-04-05 01:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:40 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.