| 报错信息 | 常见原因 | 解决办法 |
| Permission denied | 脚本没有执行权限,或者操作的文件/目录权限不足 | 给脚本可执行权限:chmod +x script.sh;确认操作目录权限 |
| /bin/bash^M: bad interpreter: No such file or directory | 文件在 Windows 下编辑,存在 CRLF 换行符 | 使用 dos2unix script.sh 转换换行符 |
| command not found | 脚本中调用的命令不存在或不在 PATH 中 | 检查 PATH,或使用命令全路径 /usr/bin/grep |
| No such file or directory | 脚本中引用的文件或路径不存在 | 确认路径正确,使用绝对路径 |
| syntax error near unexpected token | 脚本语法错误,如缺少 fi、done | 使用 bash -n script.sh 检查语法 |
| unexpected end of file | 括号、循环、条件语句没有闭合 | 检查每个 if、for、while、函数是否闭合 |
| mysqldump: command not found | 脚本运行环境 PATH 不包含命令所在路径 | 在脚本开头添加 export PATH=/usr/local/bin:/usr/bin:/bin |
| Permission denied while accessing /path/to/file | 用户权限不足,或 SELinux/AppArmor 限制 | 检查目录权限:ls -ld /path/to/file;SELinux 临时测试:sudo setenforce 0 |
| cannot create /path/to/file: Permission denied | 脚本尝试写入受保护目录 | 改为可写目录,或使用 sudo 执行 |
| Bad substitution | 使用了 Bash 特性,但用 sh script.sh 执行 | 用 bash script.sh 执行,或修改 Shebang 为 #!/bin/bash |
| Segmentation fault | 脚本调用的二进制程序崩溃 | 检查程序版本,或更新重装二进制 |
| unexpected EOF while looking for matching ... | 括号、引号、花括号未闭合 | 检查每对引号、括号、花括号是否匹配 |
| export: command not found | 使用了 export 但脚本由 /bin/sh 执行 | 改用 bash 执行脚本,或修改 Shebang 为 #!/bin/bash |