Is there a way to test whether a file, if I mark it as executable with chmod +x file
and run it with ./file
will cause the operating system to execute it (or at least attempt to execute it), without actually executing it?
So far I have:
- On Mac and Linux (or other Unix), test whether the first two bytes are
#!
. - On Linux, test whether the first four bytes are
0x7f454c46
(ELF magic number) and thee_type
(offset0x10
) isET_EXEC
(0x0002
). - On Mac, check if the file is Mach-O and
filetype
isMH_EXECUTE
. (Mach-O format is a bit more complicated and I haven't figured out the exact magic number(s) and offset forfiletype
yet.) - ...other executable formats?
Is there a way to do this more easily and correctly?