I want to use a program in the shebang, so I create a script named <myscript> with:
#!<mypgm>
I also want to be able to run <mypgm> directly from the command prompt.
<mypgm> args...
So far, no issue.
I want to be able to run <myscript> from the command prompt with arguments.
<myscript> blabla
In turn, the shebang makes <mypgm> being called with the following arguments:
<mypgm> <myscript> blabla
Now, I need to know when <mypgm> <myscript> blabla is called using the shebang, or not:
myscript blabla # uses the shebang
-or-
<mypgm> myscript blabla # directly in the command prompt.
I looked at the environment variables (edit: <=== wrong assertion (¬,¬”) ), at the process table (parent process too) but didn't find any way to make a difference.
The only thing I found so far is:
grep nonvoluntary_ctxt_switches /proc/$$/status
When this line is just after the shebang, the value is often 2 (sometimes 3) when called through the shebang, and 1 (sometimes 2) with the direct call. Being unstable and dependent on process scheduling (the number of times the process was taken off from its CPUs), I am wondering if anybody here might have a better solution.
myscript blabla
andmypgm myscript blabla
, but I take it from the comments that what you really want to distinguish between ismypgm myscript blabla
(potentially as a result of the shebang) andmypgm otherargs
, is that right? – Stephen Kitt May 06 '19 at 12:12mypgm
’s perspective,myscript blabla
ends up beingmypgm myscript blabla
, so I don’t understand what distinction you’re trying to make. – Stephen Kitt May 06 '19 at 12:24Small theoretical example. Let say I want to create a calculator. I may call it from the command prompt with: "calc PI + 1". 3 arguments. Now, I start supporting shebang to create files with calculations. So now, by lack of luck, if file PI does exist, from the command prompt, "calc PI + 1" will try to interpret what in file PI.
– Jacques May 06 '19 at 12:33mypgm myscript blabla
(run as such explicitly) while allowingmyscript blabla
(with the shebang), is that right? – Stephen Kitt May 06 '19 at 12:45myscript blabla
andmypgm myscript blabla
for you? How do you distinguish betweenmypgm myscript blabla
andmypgm otherargs
? I’m trying to understand what you’d do with the information you’re asking for, once you have it. – Stephen Kitt May 06 '19 at 13:08calc myscript blah
, how are you going to differenciate betweencalc PI
wherePI
is a script, andcalc PI + 1
? (This is why most tools use an option for scripts, e.g.awk -f myscript
.) – Stephen Kitt May 06 '19 at 13:38calc
ormyprog
does not change the (non)existence of a file named "PI". Either way, the behavior of the command is conditional upon whether the file exists or not, and as Stephen suggests, it's far better to explicitly docalc -f PI
where the calculation is stored in a file namedPI
instead of on the command line. – Monty Harder May 06 '19 at 21:54