Help with the #!/bin/whatever line in scripts
Author Message
Posted : Sat, 05 July 2008 21:11:34
Subject : Help with the #!/bin/whatever line in scripts
I'm writing a lexer for a custom scripting language, and I want it to parse the first line of the scripts the same way the shell/kernel does. I'm wondering if anyone has the EXACT specification of what is legal/illegal in that area. Or information on where it's parsed. I am specifically talking about lines like this: #!/bin/bash bash code here Or, you can specify any executable in the first line, with parameters, for example: #!/home/smlefo/test -a -b this is a custom script Which will execute the 'test' program, with the parameters -a and -b, along with the filename. I'm curious as to where this first line is parsed (is it part of the ELF specification? or is interpreted by the shell, or somewhere in the kernel?). Also, during my investigation of what is legal, I found out that the absolute path can't have any spaces... so for example: #!/home/smlefo/test directory/test -a -b blah Will obviously execute the file /home/smlefo/test, with the parameters directory/test, -a, and -b. But what if I want it to interpret "test directory" as an actual directory? I tried: #!/home/smlefo/test\ directory/test -a -b blah But that failed with the error: bash: ./ts: /home/smlefo/test\: bad interpreter: No such file or directory So... if anyone has any hints as to where I can find the exact specification of this, that would be very helpful. Thank you. ~Sean
Johannes Truschnigg
Posted : Sun, 13 July 2008 18:58:02
Subject : Help with the #!/bin/whatever line in scripts
Install and unroll a recent kernel source tree tarball, and have a look at /usr/src/linux/fs/binfmt_script.c That's where shebang and interpreter parsing apparently is done.
smlefo
Posted : Thu, 17 July 2008 01:24:39
Subject : Help with the #!/bin/whatever line in scripts
Sweet! Exactly what I was looking for. Thank you kind sir.