Regular expression for matching Unix shebang lines at the beginning of files
88
Build a utility that analyzes script files to extract and categorize their interpreter information from shebang lines.
Your task is to implement a script analyzer with the following functionality:
Parse Script Content: Accept a script file's content as a string and extract interpreter information from its shebang line.
Extract Interpreter Details: From the shebang line, extract:
#! prefix)#! prefix)Categorize Scripts: Determine the script type based on the interpreter. Support these categories:
node - for Node.js scriptspython - for Python scripts (python, python2, python3)shell - for shell scripts (bash, sh, zsh)other - for any other interpreternone - for files without a valid shebangHandle Edge Cases: Properly handle:
#!/usr/bin/env node --experimental-modules)/bin/bash vs env-based like /usr/bin/env bash)Create a module that exports a function analyzeScript(content) which takes a script's content as a string and returns an object with the following structure:
{
fullShebang: string | null, // Complete shebang line or null
interpreterPath: string | null, // Path without #! or null
interpreterName: string | null, // Base interpreter name or null
scriptType: string // Category: 'node', 'python', 'shell', 'other', 'none'
}analyzer.test.jsimport { analyzeScript } from './analyzer.js';
// Test 1: Node.js script with env
const nodeScript = '#!/usr/bin/env node\nconsole.log("hello");';
const nodeResult = analyzeScript(nodeScript);
console.assert(nodeResult.fullShebang === '#!/usr/bin/env node');
console.assert(nodeResult.interpreterPath === '/usr/bin/env node');
console.assert(nodeResult.interpreterName === 'node');
console.assert(nodeResult.scriptType === 'node');
// Test 2: Python script with arguments
const pythonScript = '#!/usr/bin/env python3 -u\nprint("hello")';
const pythonResult = analyzeScript(pythonScript);
console.assert(pythonResult.interpreterName === 'python3');
console.assert(pythonResult.scriptType === 'python');
// Test 3: Shell script with direct path
const shellScript = '#!/bin/bash\necho "hello"';
const shellResult = analyzeScript(shellScript);
console.assert(shellResult.fullShebang === '#!/bin/bash');
console.assert(shellResult.interpreterPath === '/bin/bash');
console.assert(shellResult.interpreterName === 'bash');
console.assert(shellResult.scriptType === 'shell');
// Test 4: No shebang
const noShebang = 'console.log("hello");';
const noShebangResult = analyzeScript(noShebang);
console.assert(noShebangResult.fullShebang === null);
console.assert(noShebangResult.interpreterPath === null);
console.assert(noShebangResult.interpreterName === null);
console.assert(noShebangResult.scriptType === 'none');
console.log('All tests passed!');Provides regular expression matching for Unix shebang lines.
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