POSIX Shell Scripts: Variables, Conditions, Loops, and APIsPOSIX Shell脚本:变量、条件、循环与API
Convert digits using tr: 0-4 → <, 6-9 → >, 5 unchanged.
使用 tr 转换数字:0-4 → <, 6-9 → >, 5 保持不变。
| Part部分 | Meaning含义 |
|---|---|
tr '01234' '<' |
Map characters 0,1,2,3,4 to <将字符 0,1,2,3,4 映射为 < |
| |
Pipe output to next command将输出通过管道传给下一个命令 |
tr '6789' '>' |
Map characters 6,7,8,9 to >将字符 6,7,8,9 映射为 > |
💡 Key Insight: tr automatically repeats the last character in SET2 to match the length of SET1. So tr '01234' '<' works the same as tr '01234' '<<<<<'.
💡 关键洞察: tr 会自动重复 SET2 的最后一个字符来匹配 SET1 的长度。所以 tr '01234' '<' 等同于 tr '01234' '<<<<<'。
Write echon.sh that prints a string N times with proper error handling.
编写 echon.sh,打印字符串 N 次,并正确处理错误。
Tells the system to use dash shell to execute this script. Must be the first line, no spaces before #.
告诉系统使用 dash shell 执行此脚本。必须是第一行,# 前不能有空格。
| Variable变量 | Meaning含义 |
|---|---|
$# |
Number of arguments参数个数 |
$1, $2, ... |
1st, 2nd, ... argument第1个、第2个...参数 |
$@ |
All arguments所有参数 |
$? |
Exit status of last command上一个命令的退出状态 |
| Operator运算符 | Meaning含义 | Example示例 |
|---|---|---|
-eq |
Equal (numbers)等于(数字) | [ $a -eq 5 ] |
-ne |
Not equal (numbers)不等于(数字) | [ $# -ne 2 ] |
-lt |
Less than小于 | [ $i -lt 10 ] |
-gt |
Greater than大于 | [ $1 -gt 0 ] |
-f |
Is a regular file是普通文件 | [ -f "$file" ] |
-d |
Is a directory是目录 | [ -d "$dir" ] |
*[!0-9]* matches if the string contains any non-digit character.
*[!0-9]* 匹配字符串中包含任何非数字字符的情况。
⚠️ Common Mistakes:
⚠️ 常见错误:
[ ] must have spaces inside: [ $i -lt 10 ] not [$i -lt 10][ ] 内部必须有空格:[ $i -lt 10 ] 而不是 [$i -lt 10]i=0 not i = 0 (no spaces!)变量赋值:i=0 而不是 i = 0(不能有空格!)if needs a fi, every case needs an esac每个 if 需要一个 fi,每个 case 需要一个 esacCategorize files by line count: small (<10), medium (10-99), large (≥100).
按行数对文件分类:小文件(<10行)、中文件(10-99行)、大文件(≥100行)。
* is a wildcard that matches all files in the current directory.
* 是通配符,匹配当前目录下的所有文件。
< redirects file to stdin, so wc only outputs the count.
< 将文件重定向到标准输入,所以 wc 只输出计数。
Captures command output into a variable.
将命令输出捕获到变量中。
Append $file to the small variable.
将 $file 追加到 small 变量。
💡 Why [ -f "$file" ]? The * wildcard matches both files AND directories. We use -f to check if it's a regular file before processing.
💡 为什么用 [ -f "$file" ]? * 通配符匹配文件和目录。我们用 -f 在处理前检查它是否是普通文件。
Fetch course data from UNSW Handbook API and filter by prefix.
从 UNSW Handbook API 获取课程数据并按前缀过滤。
| Option选项 | Meaning含义 |
|---|---|
-s |
Silent mode (no progress bar)静默模式(无进度条) |
-L |
Follow redirects跟随重定向 |
| Operator运算符 | Meaning含义 | Example示例 |
|---|---|---|
. |
Current object当前对象 | .data |
[] |
Iterate array遍历数组 | .results[] |
| |
Pipe (jq internal)管道(jq内部) | .data | .results |
select() |
Filter过滤 | select(.type == "Course") |
startswith() |
String starts with字符串以...开头 | startswith("COMP") |
map() |
Transform array elements转换数组元素 | map(ascii_downcase) |
any() |
Check if any matches检查是否有任何匹配 | any(. == "x") |
💡 Debugging Tips:
💡 调试技巧:
bash -n script.sh to check syntax运行 bash -n script.sh 检查语法set -x at the top to trace execution在顶部添加 set -x 跟踪执行echo "DEBUG: $var" to see variable values使用 echo "DEBUG: $var" 查看变量值| Pattern模式 | When to Use何时使用 | Example示例 |
|---|---|---|
if [ ] |
True/false conditions真/假条件 | if [ $x -gt 5 ] |
case |
Multiple value matching多值匹配 | case "$1" in ... esac |
while |
Loop until condition false循环直到条件为假 | while [ $i -lt $n ] |
for |
Iterate over items遍历项目 | for file in * |
$() |
Command substitution命令替换 | lines=$(wc -l < file) |
$((expr)) |
Arithmetic算术运算 | i=$((i + 1)) |