Shelljs执行Shell脚本报“not found”错误求助排查
Hey, let's break down why your ping.sh runs perfectly in the terminal but throws a "not found" error when called through ShellJS. Here are the top culprits and how to fix them:
1. Missing curl Due to PATH Differences
When you run the script directly in your terminal, your user's full PATH environment variable includes the directory where curl lives. But when ShellJS executes the script, it runs in a non-interactive subshell with a stripped-down PATH—so it might not be able to locate curl.
How to Verify
Add this line at the very top of ping.sh to print the current PATH:
echo "Current PATH: $PATH"
Compare the output when running ./ping.sh 8080 directly vs. via ShellJS. If the ShellJS version is missing paths like /usr/bin or /usr/local/bin (where curl is usually located), that's the issue.
Fixes
- Use the absolute path to
curlin your script. First find its path withwhich curlin your terminal (e.g.,/usr/bin/curl), then update line 7:api_response=$(/usr/bin/curl --write-out %{http_code} --silent --output /dev/null "http://localhost:$port/ping") - Or set the full PATH explicitly in your
ping.jsbefore executing the script:const shell = require('shelljs'); // Add common bin directories to PATH shell.env.PATH = '/usr/bin:/usr/local/bin:' + shell.env.PATH; shell.exec('./ping.sh 8080');
2. Working Directory Mismatch
If your ping.js isn't in the same folder as ping.sh, using ./ping.sh in ShellJS will look for the script in the current working directory of Node.js (not where ping.js is stored), leading to a "script not found" error.
How to Verify
Add this line to ping.js to check the working directory:
const shell = require('shelljs'); console.log('Current working directory:', shell.pwd().stdout);
If this isn't the folder where ping.sh lives, that's the problem.
Fixes
- Use the absolute path to
ping.shin yourping.js:shell.exec('/full/path/to/your/ping.sh 8080'); - Or switch to the script's directory first:
const shell = require('shelljs'); // Replace with the actual directory containing ping.sh shell.cd('/path/to/ping-sh-directory'); shell.exec('./ping.sh 8080');
3. Bash Syntax vs. POSIX Shell Compatibility
Your script uses (( ... ))—a Bash-specific syntax for arithmetic operations. By default, ShellJS might use sh (the POSIX-compliant shell) instead of Bash, and sh doesn't support (( ... )). This can throw an error that might look like "not found" (the shell misinterprets the syntax as a command).
How to Verify
Try running the script with sh directly:
sh ping.sh 8080
If it throws an error, this is the issue.
Fixes
- Add a shebang at the top of
ping.shto specify it should run with Bash:#!/bin/bash - Then explicitly call Bash in your
ping.js:shell.exec('bash ./ping.sh 8080');
4. Permissions (Less Likely, but Worth Checking)
While you can run the script directly, double-check that the execution permissions are set correctly (in case ShellJS runs under a different user context):
ls -l ping.sh
You should see x in the permissions (e.g., -rwxr-xr-x). If not, add execute permissions:
chmod +x ping.sh
内容的提问来源于stack exchange,提问作者Mehmet Ataş

