## 一些零散的点

• bash中双引号和单引号的区别

• bangbang

• bash 中的特殊变量
• 如何忽略命令的输出

## globbing

• 任意字符：*
• 单个字符：?
• 使用{}给定可选元素的集合。

## bash 中的函数

• 如何写函数
• 如何在bash中导入脚本中的函数

## for-loop

### until

untilwhile的用法一致，不同点在于：

• while是CONDITION为true执行，当false是退出循环
• until是CONDITION为false执行，当true时退出循环

## 数学表达式

• 使用expr，如c=$(expr 1 + 1); echo$c，注意操作数与操作符之间都是有空格的。
• 使用let，如c=1; let c=$c+1; echo$c，注意操作数与操作符之间没有空格。
• 使用双括号(())，就像上面看到的那样：c=1; echo $((c += 1)); echo$c。这时候，操作数与操作符之间的空格可有可无。

## 调试工具

shellcheck可以用来帮助静态分析shell脚本。用法：

## 几个有用的命令

### 查找文件 find

find还可以通过-exec来接后续处理，如：

### locate

• locate只支持按名字查找，find可以更加多样
• locate通过周期性更新的database来查找，时效性不如find
• locate使用更简单，默认会查找所有符合要求的文件，find一般是查找给定路径下的文件

### 在文件中查找字符串 grep

grep 用来在文件中正则匹配字符串，比如某个变量或函数定义啥的。grep命令很有用，在后续课程中还会着重介绍。

find一样，grep也有一些更好用的替代品，如rgagack等。

### 查找历史命令 history

history可以打印历史的shell命令，和grep配合能够找到历史上曾经用过的某给定命令。不过这个我在使用zsh的时候，一般是通过光标上下键来联想查找的。

### 关于目录

• 查看目录内容：tree（最经典），brootnnnranger
• 跳转目录：autojump（在用），fasd

## 课后习题

### 关于ls的用法

• Includes all files, including hidden files：ls -al
• Sizes are listed in human readable format (e.g. 454M instead of 454279954)：ls -lh
• Files are ordered by recency：ls -lt
• Output is colorized：ls -l --color （zsh自动colorized，所以这个没有验证）

### bash函数

Write bash functions marco and polo that do the following. Whenever you execute marco the current working directory should be saved in some manner, then when you execute polo, no matter what directory you are in, polo should cd you back to the directory where you executed marco. For ease of debugging you can write the code in a file marco.sh and (re)load the definitions to your shell by executing source marco.sh.

### 循环和程序返回值判断

Say you have a command that fails rarely. In order to debug it you need to capture its output but it can be time consuming to get a failure run. Write a bash script that runs the following script until it fails and captures its standard output and error streams to files and prints everything at the end. Bonus points if you can also report how many runs it took for the script to fail.

### xargs和管道

As we covered in lecture find’s -exec can be very powerful for performing operations over the files we are searching for. However, what if we want to do something with all the files, like creating a zip file? As you have seen so far commands will take input from both arguments and STDIN. When piping commands, we are connecting STDOUT to STDIN, but some commands like tar take inputs from arguments. To bridge this disconnect there’s the xargs command which will execute a command using STDIN as arguments. For example ls | xargs rm will delete the files in the current directory.

Your task is to write a command that recursively finds all HTML files in the folder and makes a zip with them. Note that your command should work even if the files have spaces (hint: check -d flag for xargs)

#### 实现

-print0-0（是数字0）配合，具体可以参考man xargs中的内容。

### 命令组合

Write a command or script to recursively find the most recently modified file in a directory. More generally, can you list all files by recency?