Regular expression for matching Unix shebang lines at the beginning of files
88
Build a utility that analyzes script files to extract and parse their interpreter directives (shebang lines). The tool should handle shebangs with complex interpreter specifications that include arguments and flags.
Your solution should provide a function analyzeScriptInterpreter that:
hasShebang (boolean): whether the script has a valid shebang line at the beginningfullShebang (string | null): the complete shebang line including #!, or null if no shebanginterpreterPath (string | null): the full interpreter directive (without #!), or null if no shebanginterpreterCommand (string | null): just the interpreter executable path (first part before any spaces), or null if no shebanginterpreterArgs (string[]): array of arguments passed to the interpreter, or empty array if none or no shebangThe function should correctly handle:
#!/bin/bash#!/usr/bin/env node#!/usr/bin/env python3 -u -W ignoreGiven script #!/usr/bin/env node\nconsole.log("hello");, the function returns an object with hasShebang: true, fullShebang: "#!/usr/bin/env node", interpreterPath: "/usr/bin/env node", interpreterCommand: "/usr/bin/env", and interpreterArgs: ["node"] @test
Given script #!/usr/bin/python3 -u -W ignore\nprint("test"), the function returns an object with hasShebang: true, interpreterPath: "/usr/bin/python3 -u -W ignore", interpreterCommand: "/usr/bin/python3", and interpreterArgs: ["-u", "-W", "ignore"] @test
Given script console.log("no shebang"); (no shebang), the function returns an object with hasShebang: false and all other properties as null or empty array @test
Given script \n#!/bin/bash\necho "test" (shebang not at start), the function returns an object with hasShebang: false since the shebang must be at the very beginning @test
@generates
/**
* Analyzes a script to extract interpreter information from its shebang line.
*
* @param {string} scriptContent - The content of the script file
* @returns {Object} An object containing:
* - hasShebang: boolean indicating if a valid shebang exists at the start
* - fullShebang: the complete shebang line including #!, or null
* - interpreterPath: the interpreter directive without #!, or null
* - interpreterCommand: just the executable path (before first space), or null
* - interpreterArgs: array of arguments passed to the interpreter
*/
function analyzeScriptInterpreter(scriptContent) {
// IMPLEMENTATION HERE
}
module.exports = { analyzeScriptInterpreter };Provides regular expression for matching and extracting shebang lines from scripts.
Install with Tessl CLI
npx tessl i tessl/npm-shebang-regexdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9