shell-scripting
Scannednpx machina-cli add skill chaterm/terminal-skills/shell-scripting --openclawShell 脚本编写
概述
Bash 脚本编写、调试、最佳实践等技能。
基础语法
脚本结构
#!/bin/bash
# 脚本描述
# Author: name
# Date: 2024-01-01
set -euo pipefail # 严格模式
# 变量定义
VAR="value"
readonly CONST="constant"
# 主逻辑
main() {
echo "Hello, World!"
}
main "$@"
变量
# 定义变量
name="value"
name='literal value' # 不解析变量
# 使用变量
echo $name
echo ${name}
echo "${name}_suffix"
# 默认值
${var:-default} # 未设置时使用默认值
${var:=default} # 未设置时赋值并使用
${var:+value} # 已设置时使用 value
${var:?error message} # 未设置时报错
# 字符串操作
${#var} # 长度
${var:0:5} # 子串
${var#pattern} # 删除前缀
${var%pattern} # 删除后缀
${var/old/new} # 替换
数组
# 定义数组
arr=(a b c d)
arr[0]="first"
# 访问数组
${arr[0]} # 第一个元素
${arr[@]} # 所有元素
${#arr[@]} # 数组长度
${!arr[@]} # 所有索引
# 遍历数组
for item in "${arr[@]}"; do
echo "$item"
done
流程控制
条件判断
# if 语句
if [[ condition ]]; then
commands
elif [[ condition ]]; then
commands
else
commands
fi
# 条件表达式
[[ -f file ]] # 文件存在
[[ -d dir ]] # 目录存在
[[ -z "$var" ]] # 变量为空
[[ -n "$var" ]] # 变量非空
[[ "$a" == "$b" ]] # 字符串相等
[[ "$a" != "$b" ]] # 字符串不等
[[ $a -eq $b ]] # 数字相等
[[ $a -lt $b ]] # 小于
[[ $a -gt $b ]] # 大于
# 逻辑运算
[[ cond1 && cond2 ]] # 与
[[ cond1 || cond2 ]] # 或
[[ ! cond ]] # 非
循环
# for 循环
for i in 1 2 3 4 5; do
echo $i
done
for i in {1..10}; do
echo $i
done
for ((i=0; i<10; i++)); do
echo $i
done
for file in *.txt; do
echo "$file"
done
# while 循环
while [[ condition ]]; do
commands
done
# 读取文件
while IFS= read -r line; do
echo "$line"
done < file.txt
# until 循环
until [[ condition ]]; do
commands
done
case 语句
case "$var" in
pattern1)
commands
;;
pattern2|pattern3)
commands
;;
*)
default commands
;;
esac
函数
定义函数
# 方式1
function_name() {
local var="local variable"
echo "Arguments: $@"
echo "First arg: $1"
echo "Arg count: $#"
return 0
}
# 方式2
function function_name {
commands
}
# 调用函数
function_name arg1 arg2
# 获取返回值
result=$(function_name)
常用函数模板
# 日志函数
log() {
local level=$1
shift
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$level] $*"
}
log INFO "This is info message"
log ERROR "This is error message"
# 错误处理
die() {
echo "ERROR: $*" >&2
exit 1
}
# 确认函数
confirm() {
read -p "$1 [y/N] " response
[[ "$response" =~ ^[Yy]$ ]]
}
if confirm "Continue?"; then
echo "Proceeding..."
fi
输入输出
读取输入
# 读取用户输入
read -p "Enter name: " name
read -sp "Enter password: " password # 隐藏输入
read -t 10 -p "Quick! " answer # 超时
# 读取文件
while IFS= read -r line; do
echo "$line"
done < file.txt
重定向
# 输出重定向
command > file # 覆盖
command >> file # 追加
command 2> error.log # 错误输出
command > file 2>&1 # 合并输出
command &> file # 同上
# 输入重定向
command < file
# Here Document
cat << EOF
多行文本
变量: $var
EOF
cat << 'EOF' # 不解析变量
原始文本
EOF
调试技巧
调试选项
# 启用调试
set -x # 打印执行的命令
set -v # 打印读取的行
set -e # 出错即退出
set -u # 未定义变量报错
set -o pipefail # 管道错误传递
# 组合使用
set -euxo pipefail
# 调试特定部分
set -x
# 调试代码
set +x
调试工具
# 语法检查
bash -n script.sh
# 调试运行
bash -x script.sh
# shellcheck 静态分析
shellcheck script.sh
最佳实践
脚本模板
#!/bin/bash
#
# Script: script_name.sh
# Description: Brief description
# Author: Your Name
# Date: 2024-01-01
#
set -euo pipefail
# Constants
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_NAME="$(basename "$0")"
# Logging
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"; }
error() { echo "[ERROR] $*" >&2; }
die() { error "$*"; exit 1; }
# Usage
usage() {
cat << EOF
Usage: $SCRIPT_NAME [options] <arguments>
Options:
-h, --help Show this help message
-v, --verbose Enable verbose mode
Examples:
$SCRIPT_NAME -v input.txt
EOF
}
# Parse arguments
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
exit 0
;;
-v|--verbose)
VERBOSE=true
shift
;;
*)
ARGS+=("$1")
shift
;;
esac
done
}
# Main
main() {
parse_args "$@"
# Your logic here
log "Starting $SCRIPT_NAME"
}
main "$@"
故障排查
| 问题 | 解决方法 |
|---|---|
| 语法错误 | bash -n script.sh 检查 |
| 变量未定义 | 使用 set -u 或 ${var:-} |
| 空格问题 | 变量加引号 "$var" |
| 管道错误被忽略 | 使用 set -o pipefail |
| 调试困难 | 使用 set -x 或 shellcheck |
Source
git clone https://github.com/chaterm/terminal-skills/blob/main/linux/shell-scripting/SKILL.mdView on GitHub Overview
This skill covers writing Bash shell scripts for Linux automation, from small helpers to larger tooling. You’ll implement a clear script structure, enable strict mode (set -euo pipefail), manage variables and arrays, control flow, and input/output, all while following best practices and using debugging aids like shellcheck.
How This Skill Works
You author scripts with a Bash interpreter (shebang: #!/bin/bash). You define variables and arrays, organize code into functions, and control flow with if, case, for, while, and until. By enabling strict mode (set -euo pipefail) and proper quoting, you ensure predictable error handling and safe word-splitting. You leverage parameter expansion, redirection, and here-docs to handle inputs/outputs, and you validate commands and inputs with debugging tools like bash -n, bash -x, and shellcheck.
When to Use It
- Automating repetitive admin tasks (user provisioning, backups, log rotation)
- Wrapping CLI tools with robust, testable pipelines
- Batch processing of files and logs (parsing, transformation, reporting)
- Startup and maintenance scripts (service checks, environment setup)
- Developer workstation automation (setup scripts, data migrations)
Quick Start
- Create a script with a proper header and strict mode, then wire a main() entry point: ```bash #!/bin/bash set -euo pipefail main() { echo "Hello, World!" } main "$@" ```
- Parse arguments and handle errors using functions and local variables: ```bash #!/bin/bash set -euo pipefail parse_args() { while [[ $# -gt 0 ]]; do case $1 in -h|--help) show_help; exit 0 ;; -v|--verbose) VERBOSE=true; shift ;; *) ARGS+=("$1"); shift ;; esac done } show_help() { echo "Usage: $0 [-v] [files...]"; } parse_args "$@" ```
- Run safely and debug with static analysis: ```bash bash -n script.sh # syntax check bash -x script.sh # trace execution for debugging shellcheck script.sh # static analysis for common issues ```
- bestPractices : ["Enforce strict mode with set -euo pipefail and define IFS to handle whitespace.","Quote all variable expansions: "${var}" and use printf for output instead of echo.","Keep functions small, use local variables, and return meaningful exit codes.","Use traps for cleanup on signals and errors, and centralize error handling with a die() helper.","Prefer getopts or a robust case statement for argument parsing; validate inputs early."]
- realWorldExamples ["Log file processor that reads line-by-line with IFS= read -r line and aggregates statistics.","Daily backup wrapper using rsync with rotation and a trap to cleanup partial backups.","User onboarding script that creates home directories, sets permissions, and initializes config files.","CLI wrapper that formats and prints command output with consistent logging and colored status." ]
- faq":[{
Best Practices
- Enforce strict mode with set -euo pipefail and a careful IFS
- Quote variables: "${var}" to prevent word splitting and globbing
- Use printf for predictable output and log formatting
- Declare constants with readonly and local variables inside functions
- Use trap and cleanup functions to gracefully handle signals and exits
Example Use Cases
- Log file processor that parses and summarizes access logs
- Backup orchestrator that validates sources, runs rsync, and rotates backups
- CLI wrapper that standardizes tool output and exposes a stable API
Frequently Asked Questions
Related Skills
file-operations
chaterm/terminal-skills
Linux file and directory operations
user-permissions
chaterm/terminal-skills
Linux user and permission management
cron
chaterm/terminal-skills
定时任务管理
convex-cron-jobs
waynesutton/convexskills
Scheduled function patterns for background tasks including interval scheduling, cron expressions, job monitoring, retry strategies, and best practices for long-running tasks
hook-factory
alirezarezvani/claude-code-skill-factory
Generate production-ready Claude Code hooks with interactive Q&A, automated installation, and enhanced validation. Supports 10 templates across 7 event types for comprehensive workflow automation.
workflow-setup
athola/claude-night-market
Configure GitHub Actions CI/CD workflows for automated testing, linting, and deployment. Use for CI/CD setup and quality automation. Skip if CI/CD configured or using different platform.