[plug] Running a script to parse a script with args
Bernard Blackham
bernard at blackham.com.au
Fri Aug 29 06:34:41 WST 2003
> >And then in your data file, use "#!/my/perl/script some args" on the
> >first line? (and of course tell your perl script to discard the
> >first line).
>
> This, being the obvious, is what I tried first - but it didn't work.
> Then again, that was several weeks ago - maybe I stuffed up something
> obvious and should go back and try it again. ^_^
Hmmm, I did try it myself before I posted and claimed it worked
yesterday... though I must've been so certain about it that I fooled
myself.
Here's what I've come up with this time, in full. The arguments (if
any are passed in the #!) come as one single argument as you've
probably discovered, and the last argument on the command line will
be the actual script name (or nothing if it was called directly
without params).
You probably want to also put a condition in the script that it
chews up the first line of input if it starts with a #! for tidiness :)
--- /tmp/perlscript
#!/usr/bin/perl
print "Arguments:";
foreach my $i (@ARGV) { print " -- $i"; }
print "\n";
$input_fn = $ARGV[$#ARGV] || "-";
open (INPUT, $input_fn) || die;
while (<INPUT>) { print; }
close INPUT;
---
--- /tmp/datafile
#!/tmp/perlscript arg1 arg2 arg3
Fragamazoo.
Testing 1
2
Three.
---
Which gives:
[b at amidala /tmp]$ ./datafile
Arguments: -- arg1 arg2 arg3 -- ./datafile
#!/tmp/perlscript arg1 arg2 arg3
Fragamazoo.
Testing 1
2
Three.
Ditto for `./perlscript < datafile` and `./perlscript datafile`
HTH,
Bernard.
--
Bernard Blackham
bernard at blackham dot com dot au
More information about the plug
mailing list