(Can't comment due to missing credit)
Re bdrx's answer (https://stackoverflow.com/a/62268465/7379507): Didn't work for me, unfortunately. Looks like @ARGV is the culprit here, leaving it out does work (Perl v5.16.3):
#!/usr/bin/perl -e$_=$ARGV[0];s/[^\/]+$/python/;exec("'$_'")
Without additional args, of course. The single quotes are necessary to safeguard for paths with spaces in directory or filenames.
Re Kenneth E. Bellock's answer (https://stackoverflow.com/a/36160331/7379507) and clarifying comments: This also has issues with blanks in path parts.
This seems to (mostly) work for me (GNU Awk 4.0.2):
(SEE BELOW for a better variant with proper return code - I'm leaving this in for educational purposes)
#!/usr/bin/awk BEGIN{a=ARGV[1];b="";for(i=2;i<ARGC;i++){b=b" "ARGV[i];}sub(/[^\/]+$/,"python",a);system(sprintf("'%s'%s",a,b))}
Modifications:
- Regex substitutes everything but forward slashes
- quoting safeguards for dir and filenames with blanks
- simple additional args like -v -i seem to work, but not quoted args like -c'import sys'
- I couldn't quite grasp the \t stuff, so start the loop with i=2 and combine ARGV entries with blanks
Thanks for the inspiration. Still, quoting on the shebang remains some sort of mystery for me... ;-)
EDIT: Just noticed that I accidently dropped the "rc=system(..);exit rc" part so lose the Python process return code. And I can't get it work my modifications. Yikes!
EDIT2: Ok, the problem with the return code lies in the shebang line-length restriction. But I realized I can do without sprintf():
!/usr/bin/awk BEGIN{a=ARGV[1];b="";for(i=2;i<ARGC;i++){b=b" "ARGV[i];}sub(/[^\/]+$/,"python",a);rc=system("'"a"'"b);exit rc}
Still, the problem with quoted args remains.