Shell Scripts
Automate repetitive 任务通过编写 shell scripts 使用变量, 循环, and conditionals.
你将学到
- 如何 编写 and 执行 shell script
- 变量, conditionals, and 循环 in bash
- 函数 and 实参 for reusable scripts
- 制作 scripts executable 使用 chmod
概念讲解
什么是 Shell 脚本?
A shell script is a plain text file containing a sequence of shell commands that the shell executes in order. Instead of typing the same commands over and over, you write them once in a file and run the file whenever you need. Shell scripts are the simplest form of automation and are used everywhere in DevOps — for deployments, backups, environment setup, and more.
Every shell script starts with a shebang line — #!/bin/bash — that tells the operating system which interpreter to use. Without the shebang, the system may use the wrong shell and your script will behave unexpectedly.
Variables and Conditionals
You can store values in variables without any type declaration: NAME="值". To read a variable, prefix it with a dollar sign: $NAME. Conditionals use 如果, 然后, else, fi syntax. You can test file existence with -f, directory existence with -d, and compare strings or numbers. These constructs let your script make decisions based on the environment.
Loops and Functions
Loops let you repeat actions. A for loop iterates over a list of items; a 虽然 loop runs as long as a condition is true. Functions group related commands and can accept arguments accessed via $1, $2, etc. Together, loops and functions let you write scripts that are both powerful and maintainable.
Making Scripts Executable
After writing a script, you must give it execute permission with chmod +x script.sh. Then you can run it as ./script.sh. Without the execute permission, the OS will refuse to run it. You can also run a script directly with bash script.sh, which does not require the execute permission but does require the shebang to be correct for portability.
Real-World Usage
Shell scripts are used extensively in DevOps for deployment automation, environment provisioning, backup routines, and log rotation. They are the glue between different tools and services. However, for complex logic or large scripts, consider a language like Python or Go — they offer better error handling, testing, and maintainability. Use shell scripts for simple automation and glue code, and reach for a full language when the script grows beyond a few dozen lines or needs error handling beyond simple exit codes.
示例代码
#!/bin/bash
# A deployment helper script
APP_NAME="myapp"
ENVIRONMENT=${1:-staging} # first argument, default "staging"
echo "Deploying $APP_NAME to $ENVIRONMENT..."
if [ ! -f "Dockerfile" ]; then
echo "Error: Dockerfile not found!"
exit 1
fi
for service in api web worker; do
echo " Building $service..."
docker build -t "$APP_NAME-$service:latest" "./$service"
done
echo "Deployment to $ENVIRONMENT complete."
这个 script demonstrates 变量, 默认实参, conditionals, and 循环. 它检查 for Dockerfile, 然后构建 three service 镜像 in 循环.
动手试一试
- Cron Parser
Parse cron 表达式 used in shell automation.
- Crontab Generator
Generate crontab 条目 for scheduled scripts.
- Regex Tester
测试 regex 模式 for grep and sed in scripts.
常见错误
错误写法
Forgetting shebang 行
正确做法
总是开始 scripts 使用 #!/bin/bash (or #!/usr/bin/环境 bash for portability). 不使用它, script 可能运行 in 不同 shell 使用不同语法.
错误写法
不 quoting 变量
正确做法
Always quote variables: "$VAR" not $VAR. Unquoted variables break on spaces and empty values, causing subtle bugs.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。