Regular expression for matching Unix shebang lines at the beginning of files
88
Build a script file validation tool that checks whether uploaded text files are valid executable scripts. The validator must detect whether files have proper shebang lines and classify the validation results.
Your validator must check if a file is a valid executable script by verifying that it starts with a shebang line. A valid shebang line:
#)#! followed by an interpreter pathThe validator should classify files into three categories:
Your validator should process an array of file content strings and return a classification result object with three arrays:
{
valid: ['file content 1', ...], // Files with proper shebangs at the start
invalid: ['file content 2', ...], // Files with misplaced shebangs
nonScripts: ['file content 3', ...] // Files without any shebang
}@generates
/**
* Validates and classifies file contents based on shebang line presence and position.
*
* @param {string[]} fileContents - Array of file content strings to validate
* @returns {Object} Classification object with valid, invalid, and nonScripts arrays
*/
function validateScripts(fileContents) {
// IMPLEMENTATION HERE
}
module.exports = { validateScripts };#!/bin/bash\necho "test", it is classified as valid @test\n#!/usr/bin/env node\nconsole.log(), it is classified as invalid @test #!/bin/sh\nls, it is classified as invalid @testconsole.log("hello world"), it is classified as non-script @testProvides regular expression for matching shebang lines in Unix 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