是的,Linux 系统原生支持运行 .sh
(Shell Script) 脚本文件。以下是详细说明和操作指南:
chmod +x
添加)。#!/bin/bash
)。chmod +x script.sh # 添加可执行权限
./script.sh # 执行脚本(需在脚本所在目录)
bash script.sh # 使用 bash 解释器
sh script.sh # 使用 sh 解释器(兼容性更好)
/path/to/script.sh # 绝对路径执行
# 错误提示:Permission denied
chmod +x script.sh # 添加权限后重试
# 错误提示:bad interpreter: No such file or directory
# 修正脚本首行,例如:
#!/usr/bin/env bash # 兼容性更好的写法
# 错误提示:^M 或语法错误
sed -i 's/\r$//' script.sh # 转换换行符为LF格式
bash -x script.sh # 打印每条执行的命令
bash -n script.sh # 仅检查语法错误(不执行)
bash
、zsh
、dash
等(通过首行 #!
指定)。./script.sh > log.txt 2>&1
)。通过以上步骤,你可以轻松在 Linux 中运行和调试 .sh
脚本。遇到具体问题时,可结合错误信息进一步排查。