https://missing.csail.mit.edu/
https://missing-semester-cn.g...
https://www.bilibili.com/vide...
$0
- 脚本名$1
到 $9
- 脚本的参数。 $1
是第一个参数,依此类推。$@
- 全部参数$#
- 参数个数$?
- 前一个命令的返回值$$
- 当前脚本的进程识别码!!
- 完整的上一条命令,包括参数。常见应用:当你由于权限不足执行命令失败时,可使用 sudo !!
再尝试一次。$_
- 上一条命令的最后一个参数。若是你正在使用的是交互式shell,你能够经过按下 Esc
以后键入 .
来获取这个值。一个冷门的相似特性是 进程替换 ( process substitution ), <( CMD )
会执行 CMD
并将结果输出到一个临时文件中,并将 <( CMD )
替换成临时文件名。这在咱们但愿返回值经过文件而不是STDIN传递时颇有用。例如, diff <(ls foo) <(ls bar)
会显示文件夹 foo
和 bar
中文件的区别。html
convert image.{png,jpg} # 会展开为 convert image.png image.jpg cp /path/to/project/{foo,bar,baz}.sh /newpath # 会展开为 cp /path/to/project/foo.sh /path/to/project/bar.sh /path/to/project/baz.sh /newpath # 也能够结合通配使用 mv *{.py,.sh} folder # 会移动全部 *.py 和 *.sh 文件 mkdir foo bar # 下面命令会建立foo/a, foo/b, ... foo/h, bar/a, bar/b, ... bar/h这些文件 touch {foo,bar}/{a..h} touch foo/x bar/y # 显示foo和bar文件的不一样 diff <(ls foo) <(ls bar) # 输出 # < x # --- # > y
注意,脚本并不必定只有用bash写才能在终端里调用。好比说,这是一段Python脚本,做用是将输入的参数倒序输出:python
#!/usr/local/bin/python import sys for arg in reversed(sys.argv[1:]): print(arg)
shell知道去用python解释器而不是shell命令来运行这段脚本,是由于脚本的开头第一行的 shebang)。linux
在 shebang
行中使用 env
命令是一种好的实践,它会利用环境变量中的程序来解析该脚本,这样就提升来您的脚本的可移植性。env
会利用咱们第一节讲座中介绍过的PATH
环境变量来进行定位。 例如,使用了env
的shebang看上去时这样的#!/usr/bin/env python
。git
编写 bash
脚本有时候会很别扭和反直觉。例如 shellcheck 这样的工具能够帮助你定位sh/bash脚本中的错误。例如:github
locate 和 find 的对比。shell
grep 的例子:windows
rg 的例子:bash
# 查找全部使用了 requests 库的文件 rg -t py 'import requests' # 查找全部没有写 shebang 的文件(包含隐藏文件) rg -u --files-without-match "^#!" # 查找全部的foo字符串,并打印其以后的5行 rg foo -A 5 # 打印匹配的统计信息(匹配的行和文件的数量) rg --stats PATTERN
有一点值得注意,输入命令时,若是您在命令的开头加上一个空格,它就不会被加进shell记录中。当你输入包含密码或是其余敏感信息的命令时会用到这一特性。若是你不当心忘了在前面加空格,能够经过编辑。bash_history
或 .zhistory
来手动地从历史记录中移除那一项。koa
Oh-my-zsh? 新手上路看这篇:Setting up Windows Terminal, WSL and Oh-my-Zsh
macro.sh:ide
macro() { macro_dir=$(pwd) echo "I am in $macro_dir" | tee /mnt/f/code/learn/missing-semester/l2-shell-tools/macro.txt }
polo.sh:
polo() { cd "$macro_dir" || exit macro }
ex3_solution.sh:
#!/usr/bin/env bash ./ex3_problem.sh > ex3_result.txt 2> ex3_result.txt state=$? count=0 while [[ state -eq 0 ]]; do ./ex3_problem.sh >> ex3_result.txt 2>> ex3_result.txt state=$? count=$((count + 1)) done cat ex3_result.txt echo "ex3_problem ran $count times before failure"
$ tree ex4_html_folder ex4_html_folder ├── 1.html ├── 1.txt ├── a │ ├── a 1.html │ ├── a 1.txt │ ├── a 2.txt │ └── a 3.txt └── b ├── b 1.html ├── b 2.html └── b 3.html 2 directories, 9 files
参考 tldr xargs
给出的用法示例:
- Delete all files with a .backup extension (-print0 uses a null character to split file names, and -0 uses it as delimiter): find . -name {{'*.backup'}} -print0 | xargs -0 rm -v
tldr tar
给出了 tar 命令的用法示例:
- [c]reate an archive from [f]iles: tar cf {{target.tar}} {{file1}} {{file2}} {{file3}} - E[x]tract a (compressed) archive [f]ile into the target directory: tar xf {{source.tar[.gz|.bz2|.xz]}} --directory={{directory}} - Lis[t] the contents of a tar [f]ile [v]erbosely: tar tvf {{source.tar}}
所以本题解答以下:
find . -name "*.html" -print0 | xargs -0 tar cf html.tar
验证一下:
$ tar tvf html.tar -rwxrwxrwx yzj/yzj 0 2021-01-29 15:00 ./ex4_html_folder/1.html -rwxrwxrwx yzj/yzj 0 2021-01-29 15:25 ./ex4_html_folder/a/a 1.html -rwxrwxrwx yzj/yzj 0 2021-01-29 15:25 ./ex4_html_folder/b/b 1.html -rwxrwxrwx yzj/yzj 0 2021-01-29 15:25 ./ex4_html_folder/b/b 2.html -rwxrwxrwx yzj/yzj 0 2021-01-29 15:25 ./ex4_html_folder/b/b 3.html $ mkdir ex4_html_folder_extracted $ tar xf html.tar --directory=ex4_html_folder_extracted $ tree ex4_html_folder_extracted ex4_html_folder_extracted └── ex4_html_folder ├── 1.html ├── a │ └── a 1.html └── b ├── b 1.html ├── b 2.html └── b 3.html 3 directories, 5 files
上面的解法是把 find 命令的输出的分隔符,由本来的换行符变成了 null,而后让 xargs 也用 null 做为分隔符。也能够用 -d 选项指定换行符做为分隔符,所以另解以下:
find . -name "*.html" | xargs -d "\n" tar cf html.tar
# 按最近修改顺序列出文件 $ find . -type f -print0 | xargs -0 ls -lt --color -rwxrwxrwx 1 yzj yzj 10240 Jan 29 15:27 ./html.tar -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:25 './ex4_html_folder/a/a 1.html' -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:25 './ex4_html_folder_extracted/ex4_html_folder/a/a 1.html' -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:25 './ex4_html_folder/a/a 3.txt' -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:25 './ex4_html_folder/a/a 2.txt' -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:25 './ex4_html_folder/a/a 1.txt' -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:25 './ex4_html_folder/b/b 1.html' -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:25 './ex4_html_folder/b/b 2.html' -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:25 './ex4_html_folder/b/b 3.html' -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:25 './ex4_html_folder_extracted/ex4_html_folder/b/b 1.html' -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:25 './ex4_html_folder_extracted/ex4_html_folder/b/b 2.html' -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:25 './ex4_html_folder_extracted/ex4_html_folder/b/b 3.html' -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:01 ./ex4_html_folder/1.txt -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:00 ./ex4_html_folder/1.html -rwxrwxrwx 1 yzj yzj 0 Jan 29 15:00 ./ex4_html_folder_extracted/ex4_html_folder/1.html -rwxrwxrwx 1 yzj yzj 837 Jan 29 10:14 ./ex3_result.txt -rwxrwxrwx 1 yzj yzj 291 Jan 29 10:11 ./ex3_solution.sh -rwxrwxrwx 1 yzj yzj 205 Jan 29 09:58 ./ex3_problem.sh -rwxrwxrwx 1 yzj yzj 58 Jan 29 09:52 ./macro.txt -rwxrwxrwx 1 yzj yzj 49 Jan 29 09:48 ./polo.sh -rwxrwxrwx 1 yzj yzj 129 Jan 29 09:44 ./macro.sh -rwxrwxrwx 1 yzj yzj 50 Jan 28 21:41 ./mcd.sh -rwxrwxrwx 1 yzj yzj 509 Jan 28 21:10 ./example.sh # 找到最近修改的文件 $ find . -type f -print0 | xargs -0 ls -lt --color | head -n1 -rwxrwxrwx 1 yzj yzj 10240 Jan 29 15:27 ./html.tar