execute_bash_script
Run bash script files in WSL2 Linux environments with optional arguments, working directory, and timeout settings for automated Linux operations.
Instructions
Execute a bash script file in WSL2 Linux environment
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| scriptPath | Yes | Path to the bash script file | |
| args | No | Arguments to pass to the script (optional) | |
| workingDirectory | No | Working directory for the script (optional) | |
| timeout | No | Timeout in milliseconds (optional, uses config default) |
Implementation Reference
- src/index.js:379-446 (handler)The core handler function that executes the specified bash script in the WSL Linux environment. It constructs a WSL command, handles arguments escaping, executes via execAsync, and returns structured output with success status, stdout, stderr.async executeBashScript(args) { const { scriptPath, args: scriptArgs = [], workingDirectory = ".", timeout = this.config?.scriptTimeout || 60000 } = args; if (!scriptPath || typeof scriptPath !== "string") { throw new Error("Script path is required and must be a string"); } if (!this.wslDistribution) { throw new Error("WSL distribution not configured"); } try { // Prepare arguments string const argsString = scriptArgs.map(arg => `'${arg.replace(/'/g, "'\"'\"'")}'`).join(' '); // Construct WSL command const wslCommand = `wsl -d ${this.wslDistribution} -- bash -c "cd '${workingDirectory}' && bash '${scriptPath}' ${argsString}"`; console.error(`[DEBUG] Executing script: ${wslCommand}`); const { stdout, stderr } = await execAsync(wslCommand, { timeout, maxBuffer: this.config?.maxBufferSize || 10 * 1024 * 1024, }); return { content: [ { type: "text", text: JSON.stringify({ success: true, scriptPath: scriptPath, args: scriptArgs, workingDirectory: workingDirectory, wslDistribution: this.wslDistribution, stdout: stdout || "", stderr: stderr || "", timestamp: new Date().toISOString() }, null, 2), }, ], }; } catch (error) { return { content: [ { type: "text", text: JSON.stringify({ success: false, scriptPath: scriptPath, args: scriptArgs, workingDirectory: workingDirectory, wslDistribution: this.wslDistribution, error: error.message, stdout: error.stdout || "", stderr: error.stderr || "", timestamp: new Date().toISOString() }, null, 2), }, ], }; } }
- src/index.js:187-212 (schema)Defines the tool's metadata, description, and input schema specification for validation in the ListTools response.{ name: "execute_bash_script", description: "Execute a bash script file in WSL2 Linux environment", inputSchema: { type: "object", properties: { scriptPath: { type: "string", description: "Path to the bash script file", }, args: { type: "array", items: { type: "string" }, description: "Arguments to pass to the script (optional)", }, workingDirectory: { type: "string", description: "Working directory for the script (optional)", }, timeout: { type: "number", description: "Timeout in milliseconds (optional, uses config default)", } }, required: ["scriptPath"], },
- src/index.js:284-285 (registration)Maps the tool name to its handler function in the CallToolRequestSchema switch dispatcher.case "execute_bash_script": return await this.executeBashScript(args);