I have a library - users are to create executable files, potentially with a hashbang to tell exec
which executable to use. If they omit the hashbang, then I think most systems default to /bin/sh
, I want to change that default.
So some files might start with:
#!/usr/bin/env foobar
other files might start with:
#!/usr/bin/env perl
or
#!/usr/bin/env ruby
And in some cases, the user will omit the hashbang altogether. In that case, I will still execute the file directly, and I want to default to using the foobar
executable.
In other words, I don't know what the hashbang will be in advance, and I want the default to be foobar
instead of the default being /bin/sh
, if there is no hashbang present.
I think the way to do what I am doing is to create an executable that can run exec
first, and if that fails with a particular error message, then run the script indirectly with the foobar
executable?
Something like this:
function run {
stdio=$("$1" 2>&1)
if [[ stdout/stderr matches a certain error message ]]; then
foobar $1
fi
}
My question is - does anyone know what I am talking about - and does anyone know how I can default to a particular executable if no hashbang is present? Not that it's in theory less convenient to check if there is a hashbang, and more in theory more convenient just to run it?
/bin/sh
, it’s the C library, and only for certainexec
calls. – Stephen Kitt Nov 24 '17 at 21:53