Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
如何学习 Shell
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
LI Daobing
April 06, 2008
Programming
360
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
如何学习 Shell
LI Daobing
April 06, 2008
More Decks by LI Daobing
See All by LI Daobing
How to attack TLS in PQC decade, part I
lidaobing
0
52
出了问题不要靠猜
lidaobing
40
4.1k
HTTP协议相关的若干安全问题
lidaobing
9
1.2k
Debian & Packaging
lidaobing
1
590
Java 质量保障
lidaobing
3
320
OAuth: How and Why?
lidaobing
1
180
从 Struts 迁移到 Spring MVC,以及为什么?
lidaobing
2
680
glusterfs 文件系统
lidaobing
2
220
Other Decks in Programming
See All in Programming
源内ハンズオン概要編
hideg
0
210
[PyCon KR 2026] More Variants, More Diversity for AI Accelerators
achimnol
0
110
go-spidermonkeyでAIエージェントのCode Modeを実装する
syumai
2
1.2k
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
580
AI時代に学ぶ 好きなルール 嫌いなルール Linter編
shorty5121
0
150
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
400
人間の目はかわらない、だからJPEGは30年もつ
yuzneri
12
19k
プロポーザルを書いてもらう
pvcresin
0
580
PyConJP2026_wat_Python × Signal Processing: How to Draw Pictures with Sound Using Spectrogram Art
wat
0
380
思考垂れ流し開発 ~音声入力 × AIエージェント × 開発ハーネスによる試行錯誤~
npostring
0
190
自動化したのに回らない テスト運用の壁―AI時代の品質責任と生産性
mfunaki
0
500
属人化した知識を、 AIが辿れる地図にする
pkshadeck
PRO
1
210
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.1k
Making Projects Easy
brettharned
120
6.7k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
340
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
800
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
360
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
How STYLIGHT went responsive
nonsquared
100
6.2k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.9k
Testing 201, or: Great Expectations
jmmastey
46
8.2k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Transcript
如何学习Bash Shell LI Daobing <
[email protected]
> Kingsoft Inc. 2008-04-06
目录 •命令,管道与重定向 •环境变量 •扩展与quote •流程控制 •调试与安全 •附1: 进程查看与调试工具, gdb以外的世界 •附2:
好用的命令介绍
命令,管道与重定向(1) •常用命令列表 ◦ sudo apt-get install manpages-posix; dpkg -L manpages-posix
◦ sudo apt-get install coreutils info; info coreutils ◦ help •管道 ◦标准输入(0),标准输出(1),标准错误(2) ◦将上一个程序的标准输出链接到下一个命令的 标准输入: ls | less
命令,管道与重定向(2) •重定向 ◦ls > ls.log; ls >> ls.log; ls 2>&1
◦">": 将标准输出更改为 ls.log 文件(覆盖) ▪ "2> ls.log": 将标准错误更改为 ls.log 文件(覆盖) ◦">>": 将标准输出更改为 ls.log 文件(追加) ◦"2>&1": 将标准输出的设备复制到标准错误 ◦make 2>&1 | less ▪ 1. 将标准输出更改为管道设备 (管道优先级最高,其余的重 定向指令从左往右处理) ▪ 2. 将标准输出的设备复制到标准错误 ◦参考 bash(1)(REDIRECTION) , mkfifo(1)
命令,管道与重定向(3) •特殊设备 ◦/dev/null: 空设备,读的时候会马上得到 EOF, 写操作 是空操作(黑洞),可用来抛弃输出 ◦/dev/zero: 零设备,读的时候会得到无限的 '\0',写操
作也是空操作 ◦/dev/urandom: 随机数设备,读的时候能得到随机数: $ cat /dev/urandom | head -c 16 | hexdump -C 00000000 e8 fe c0 6e 6f ec 02 89 12 d8 5c 39 da b6 df dd |...no.....\9....| 00000010
环境变量(1) •系统环境变量 ◦使用 env 查看 ◦使用 export A=1 设置 ◦会被子进程继承
◦只能为字符串 •BASH 的变量 ◦使用 set/declare 查看 ◦使用 A=1 设置 ◦不被子进程继承 ◦可以为字符串,字符串数组,整数等
扩展与quote (1) •参数的文件名扩展 ◦ls *.png; ls ?.jpg; ls [a-z]* •变量扩展:
$A, ${A} •使用其他命令的输出作为参数 ◦echo `pwd`; A=`pwd` ◦A=1.gif; convert $A `basename $A .gif`.png •其他扩展 ◦$#, $0, $1, ..., "$@", "$*"
扩展与quote (2) •quoting ◦需要quote的字符: |&;()<> space tab ! \ ◦用
\ 来 quote: \|, \& ◦用 '...': 消除所有岐义,不得包含单引号(') ◦用 "...": 与 '...' 类似,但允许变量替换($A),或者命令 替换 `pwd`,也允许使用\"\$\`。 ◦其他: "$@", "$*", $'...'
流程控制 (1) •&&, || , for, while, if, case •&&,
|| ◦ cmd1 || { echo "cmd1 failed"; exit 1; } ◦ test -f .bashrc && cat .bashrc • for ◦ for x in *.jpg; do convert "$x" "`basename $x .jpg`.png" done • while: ◦ x=1; while test $x -lt 100; do echo $x; x=`expr $x + 1`; done
流程控制 (2) •if ◦if [ `id -u` = 0 ];
then echo "you are root"; fi •case ◦ case "$TERM" in xterm*|rxvt*) PROMPT_COMMAND='...' ;; screen*) PROMPT_COMMAND='....' ;; *) ;; esac
调试与安全 (1) •bash -n: 仅检查语法,不执行 •set -x: 在执行前打印实际命令(已完成所有替换) •set -C:
防止在流重定向时覆盖文件(可放入 . bashrc, 减少误操作风险) •mktemp, tempfile: 如何使用临时文件/临时目录 •trap: 截获信号
进程查看与调试工具, gdb以外 •ps aux: 列出所有进程 •pstree -pAa: 进程树 •lsof -p
12345: 列出进程打开的文件 •lsof .bashrc: 列出文件被打开的情况 •fuser -m /dev/sdb1: 列出分区被打开的情况 •strace ls: 跟踪进程使用系统调用的情况 •strace -p 12345: 跟踪已经执行的进程
进程查看与调试工具, gdb以外 •ltrace: 跟踪动态库调用情况 •valgrind: 跟踪程序内存使用情况
好用的命令介绍 •find . -name *.hpp | xargs grep HELLO •find
. -name *.hpp -print0 | \ xargs -0 grep HELLO •find . -name *.hpp -exec grep HELLO {} \; •awk, sed: 专业的流编辑器(字符串函数) •nc: TCP/IP 网络发送/监听/代理工具,可以帮助你 在系统无法完全正常启动时输出错误报告或者输 入数据。 •autossh/tsocks: 自用的 tsocks 代理工具
好用的命令介绍 •dsh: 同时管理多台机器的工具 •keychain: ssh/pgp 信任辅助工具(假定你的 ssh 信任需要输入密码, 那么如何在脚本中安全的使 用?)
•iptraf: IP 流量监视工具
Thanks for your attention