SlideShare uma empresa Scribd logo
1 de 32
Baixar para ler offline
Using Shell
Mastering Shell
shengxuanwei
2014-01-15
想听到什么?
1. Shell基础
2. Shell应⽤用
3. Shell原理
没了,该怎么办?
1. 了解:有基础的认识,简单地模仿
2. 使⽤用:针对具体⼯工作,灵活地应⽤用
3. 掌握:从语法和原理⼊入⼿手,组合命令,形成
Shell脚本
《Linux命令⾏行⼤大全》
• 它会教你⼊入⻔门
• 了解各种类型的命令
《Shell脚本学习指南》
• 完整的Shell语法
• POSIX标准
《Advanced Bash-Scripting
Guide》
• 免费英⽂文电⼦子书
• 看名字⽐比较⾼高级
• 其实差不多
Shell三问
1. What
2. When
3. How
什么是Shell
• ⼀一个程序
• 解释性语⾔言
• 与Kernel交互
• bourne shell,bash,zsh
Shell应⽤用场景
• 环境部署
• 编程开发
• 线上观察
• 数据分析
• 系统管理
Shell特点
• Quick and dirty,糙快猛
• 做⼀一件事,并把他做好(Unix设计哲学)
• 搭积⽊木,从⼩小到⼤大,没有设计
Shell编程基础
• Unix/Linux
• Shell环境和语法
• 命令⾏行⼯工具
• 正则表达式,http://regex.alf.nu
• ⽂文本编辑器
命令
• Shell关键字 alias别名
!
• 内建命令 function函数
!
• 外部命令
bash-3.2$ type if	
if is a shell keyword
bash-3.2$ type type	
type is a shell builtin
bash-3.2$ type grep	
grep is /usr/bin/grep
bash-3.2$ type ll	
ll is an alias for ls -l
bash-3.2$ type take	
take is a shell function
常⽤用命令
• 帮助:man, type, which
• ⽂文件:mkdir, cp, mv, rm, ln, touch, chmod, sudo, find
• ⽂文本:cat, sort, uniq, cut, tr, wc, diff, tail, head, less
• grep, awk, sed
• 系统:ps, top, kill, pkill, pgrep, uname
• ⺴⽹网络:wget, ssh, scp, curl
• http://ss64.com/bash/
readlines库
• history
• !!, !str, !num
• emacs mode
• C-r, C-p, C-a, C-e, C-u, C-k, C-l
• mkdir foo && cd $_
• find . -type f -name “*.log” -cmin -30
• cat webapp.log.20140115* | grep
‘module=place’ | grep -v ‘action=list’ | wc -l
• ps aux | grep ‘lighttpd’ | awk ‘{print $2}’ |
xargs kill -9
管道与IO重定向
• 标准输⼊入(stdin)、输出(stdout)、错误(stderr)
• ⽂文件描述符, 0, 1, 2
• [n] < file
• [n] >[|] file、 [n] >> file
• &> file 、>& file 、&>> file
• /dev/null
条件测试
• test expression
• [ expression ]
• [[ expresioon ]] #bash扩展版,⽀支持正则表达式 =~,⽀支
持模式匹配 ==
• expression表达式分为⽂文件型、字符串型、整数型,逻辑
操作符
• 结合控制运算符 && 和 ||
bash-3.2$ test -f foo.txt	
bash-3.2$ [ -f foo.txt ]
变量和数组
• foo=bar #=两边不能有空格,访问时$foo
• foo=“bar 1” #值有空格等特殊字符时需要引
⽤用引⽤用
• lst[0]=1 #访问时${lst[0]}
• lst=(1 2 3 4) #访问时${lst[@]},数组⻓长度
${#lst[@]
参数扩展
• # 前提:如果foo定义了,但为空,则:
!
• ${foo-default} # 还使⽤用$foo(即为空)
• ${foo:-default} # 使⽤用default字符串,*常⽤用*
!
• ${foo=default} # 还使⽤用$foo(即为空)
• ${foo:=default} # 将$foo设置成default字符串,*常⽤用*
!
•  ${foo+default} # 使⽤用default
•  ${foo:+default} # 使⽤用$foo(即为空)
!
•  ${foo?default} #还使⽤用$foo(即为空)
•  ${foo:?default} # 使⽤用default,为作为标准错误输出的⼀一部分内容
`
• ls *.log #路径名扩展
• ls ~ #波浪线扩展
• $((1+1)) #算术扩展*
• echo {1..10}; cp foo{,.bak} #花括号扩展
• echo {$i:-1} #参数扩展*
• ls -l $(which cp) #命令扩展*
字符串匹配处理
• ${var#pattern}  # 最短头匹配截取
• ${var##pattern} # 最⼤大头匹配截取
• ${var%pattern} # 最短尾匹配截取
• ${var%%pattern} # 最⼤大尾匹配截取
!
• ${var:position}   #从左往右看,匹配position(position是整数)位置之左的,留下之后的
• ${var:(-position)} #从右往左看,匹配position位置之左的,()是为了避免和${var-default}冲突
• ${var:position:length} # 匹配position位置之前的同时,显⽰示之后的⻓长度为length
!
• ${var/pattern/replacement}  # 第⼀一次匹配的被替换,类似:sed 's/pattern/replacement/'
• ${var//pattern/replacement}  # 全局的匹配被替换,类似:sed 's/pattern/replacement/g'
• ${var#/prefix/replacement} # prefix前缀替换
• ${var%/suffix/replacement} # suffix后缀替换
特殊参数
• $#: 位置参数的数量
• $*: 所有位置参数的内容($1 $2 ...)
• $@: 所有位置参数的内容(“$1” “$2” ... )
• $?: 命令执⾏行后返回的状态
• $$: 当前进程的进程号
• $!: 后台运⾏行的最后⼀一个进程号
• $0: 当前执⾏行的进程名
• $1, $2, $3等: 位置参数
• $_: 之前执⾏行的命令的最后⼀一个参数
环境变量
• export #设置新的环境变量
• env #显⽰示所有环境变量
• set #显⽰示所有本地定义的shell变量
• unset #清除环境变量
• $PATH, $PWD, $HOME, $SHELL
if, for, while
#!/bin/bash*
**
if*[*,d*'/home/work'*];*then'
****echo*“work*dir*exist”
else*
****echo*"work*dir*not*exist"*
****mkdir*/home/work*
fi'
#!/bin/bash*
#* *
n=10*
for*((*i=0;*i<$n;*i++*))*;*do*
********echo*$i*
done!
#!/bin/bash*
*
while**read**i**j**k*;***do*
*
********echo**$i**$j**$k*
*
done*<*./txt!
#!/bin/bash*
#* *
for*i*in*`seq*1w*10`*;*do*
********echo*$i*
done!
function
• return值会作为退出值,默认是return $?
• 位置参数会被临时覆盖,$#,$@,$1
function foo {	
	 commands	
	 return	
}
foo() {	
	 commands	
	 return	
}
正则表达式
• 基本的正则表达式(BREs,Basic Regular Expression)
• 扩展的正则表达式(EREs,Extended Regular Expression)
• Perl 的正则表达式(PREs,Perl Regular Expression)
• grep
• BREs
• -E EREs
• -P PREs
• sed
• BREs
• -r EREs
• awk
• EREs
主要差异
字段 说明 BREs EREs PRCs
() 匹配表达式
不⽀支持(但可以
使⽤用(),如:
(dog)
() ()
?
匹配前⾯面的⼦子表达式 0
次或 1 次
不⽀支持(同?) ? ?
d 匹配从 0 到 ⼀一数字字符 不⽀支持 不⽀支持 d
s 匹配任何空⽩白字符, 不⽀支持 不⽀支持 s
awk
• 输⼊入流处理,经常⽤用于统计分析
• pattern模式,action操作
• BEGIN,END模式
• awk –F “t” “{print $1}”
• awk –F “t” “{printf(“%dn”,$2)}END{}”
sed
• 逐⾏行处理输⼊入,并将结果发送到屏幕
• 定址
• 可以是数字、正则表达式、或⼆二者的结合
• 命令
• d,删除
• p,打印
• s,替换
• q,退出
set 命令
• set -u # 确保变量都被初始化
• set -e # 确保捕获所有⾮非0状态
• set -n # 预读⽽而不执⾏行(交互式时不⽣生效)
• set -x # 显⽰示详细执⾏行过程
• set -o pipefail # 结合-e,捕获管道间错误
notice
• 命令执⾏行前按tab确认操作参数,尤其rm
• 修改配置⽂文件时,记得先备份,再修改
• 赋值等号两边没有空格
• 条件测试[ ]内两边有空格
• 双引号、单引号、转义
谢谢
Q & A

Mais conteúdo relacionado

Destaque

Ligji i diskriminimit
Ligji i diskriminimitLigji i diskriminimit
Ligji i diskriminimitHamza Sadrija
 
2013年京JS参会分享
2013年京JS参会分享2013年京JS参会分享
2013年京JS参会分享Jiyee Sheng
 
下一个读代码的人就是你
下一个读代码的人就是你下一个读代码的人就是你
下一个读代码的人就是你Jiyee Sheng
 
Voice marketing phone broadcast
Voice marketing phone broadcastVoice marketing phone broadcast
Voice marketing phone broadcastbrook2k2yhill
 
Voice marketing phone broadcast
Voice marketing phone broadcastVoice marketing phone broadcast
Voice marketing phone broadcastbrook2k2yhill
 
Tvi presentation 2010_india latest
Tvi presentation 2010_india latestTvi presentation 2010_india latest
Tvi presentation 2010_india latesttravelno1
 
一个顽强的bug修复经历
一个顽强的bug修复经历一个顽强的bug修复经历
一个顽强的bug修复经历Jiyee Sheng
 
下一个读代码的人就是你
下一个读代码的人就是你下一个读代码的人就是你
下一个读代码的人就是你Jiyee Sheng
 
iOS团队开发实践经验
iOS团队开发实践经验iOS团队开发实践经验
iOS团队开发实践经验Jiyee Sheng
 
2004 2 al.pdf- ligji per barazi gjinore
2004 2 al.pdf- ligji per barazi gjinore2004 2 al.pdf- ligji per barazi gjinore
2004 2 al.pdf- ligji per barazi gjinoreHamza Sadrija
 
Mac - 推开程序员的另一扇窗
Mac - 推开程序员的另一扇窗Mac - 推开程序员的另一扇窗
Mac - 推开程序员的另一扇窗Jiyee Sheng
 
アイスランド旅のしおり
アイスランド旅のしおりアイスランド旅のしおり
アイスランド旅のしおりmasahiko79
 
Bmukk ppt training_and_implementation
Bmukk ppt training_and_implementationBmukk ppt training_and_implementation
Bmukk ppt training_and_implementationNeschy
 

Destaque (18)

Citacion pleito portos de galicia
Citacion pleito portos de galiciaCitacion pleito portos de galicia
Citacion pleito portos de galicia
 
Ligji i diskriminimit
Ligji i diskriminimitLigji i diskriminimit
Ligji i diskriminimit
 
2013年京JS参会分享
2013年京JS参会分享2013年京JS参会分享
2013年京JS参会分享
 
Denimet me vdekje
Denimet   me   vdekjeDenimet   me   vdekje
Denimet me vdekje
 
下一个读代码的人就是你
下一个读代码的人就是你下一个读代码的人就是你
下一个读代码的人就是你
 
Voice marketing phone broadcast
Voice marketing phone broadcastVoice marketing phone broadcast
Voice marketing phone broadcast
 
Voice marketing phone broadcast
Voice marketing phone broadcastVoice marketing phone broadcast
Voice marketing phone broadcast
 
Tvi presentation 2010_india latest
Tvi presentation 2010_india latestTvi presentation 2010_india latest
Tvi presentation 2010_india latest
 
Tarifas portuarias 2011
Tarifas portuarias 2011Tarifas portuarias 2011
Tarifas portuarias 2011
 
Suba salarial xunta 2006
Suba salarial  xunta 2006Suba salarial  xunta 2006
Suba salarial xunta 2006
 
一个顽强的bug修复经历
一个顽强的bug修复经历一个顽强的bug修复经历
一个顽强的bug修复经历
 
下一个读代码的人就是你
下一个读代码的人就是你下一个读代码的人就是你
下一个读代码的人就是你
 
iOS团队开发实践经验
iOS团队开发实践经验iOS团队开发实践经验
iOS团队开发实践经验
 
2004 2 al.pdf- ligji per barazi gjinore
2004 2 al.pdf- ligji per barazi gjinore2004 2 al.pdf- ligji per barazi gjinore
2004 2 al.pdf- ligji per barazi gjinore
 
Mac - 推开程序员的另一扇窗
Mac - 推开程序员的另一扇窗Mac - 推开程序员的另一扇窗
Mac - 推开程序员的另一扇窗
 
正则指引
正则指引正则指引
正则指引
 
アイスランド旅のしおり
アイスランド旅のしおりアイスランド旅のしおり
アイスランド旅のしおり
 
Bmukk ppt training_and_implementation
Bmukk ppt training_and_implementationBmukk ppt training_and_implementation
Bmukk ppt training_and_implementation
 

Semelhante a Using Shell & Mastering Shell

1, shell intro
1, shell intro1, shell intro
1, shell introted-xu
 
使用Dsl改善软件设计
使用Dsl改善软件设计使用Dsl改善软件设计
使用Dsl改善软件设计mingjin
 
Linux必备知识与Unix基础文化
Linux必备知识与Unix基础文化Linux必备知识与Unix基础文化
Linux必备知识与Unix基础文化Dahui Feng
 
scrapy+sphinx搭建搜索引擎
scrapy+sphinx搭建搜索引擎scrapy+sphinx搭建搜索引擎
scrapy+sphinx搭建搜索引擎Ping Yin
 
PHP 語法基礎與物件導向
PHP 語法基礎與物件導向PHP 語法基礎與物件導向
PHP 語法基礎與物件導向Shengyou Fan
 
02.python基础
02.python基础02.python基础
02.python基础modou li
 
OpenWebSchool - 02 - PHP Part I
OpenWebSchool - 02 - PHP Part IOpenWebSchool - 02 - PHP Part I
OpenWebSchool - 02 - PHP Part IHung-yu Lin
 
Shell(bash) Scripting
Shell(bash) ScriptingShell(bash) Scripting
Shell(bash) ScriptingRobby Lee
 
Linux Binary Exploitation - Stack buffer overflow
Linux Binary Exploitation - Stack buffer overflowLinux Binary Exploitation - Stack buffer overflow
Linux Binary Exploitation - Stack buffer overflowAngel Boy
 
Linux基础
Linux基础Linux基础
Linux基础zhuqling
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeAngel Boy
 
shell script introduction
shell script introductionshell script introduction
shell script introductionJie Jin
 
Binary exploitation - AIS3
Binary exploitation - AIS3Binary exploitation - AIS3
Binary exploitation - AIS3Angel Boy
 
Bash入门基础篇
Bash入门基础篇Bash入门基础篇
Bash入门基础篇Zhiyao Pan
 
Introduce to Linux command line
Introduce to Linux command lineIntroduce to Linux command line
Introduce to Linux command lineWen Liao
 
Automate with Ansible basic (2/e)
Automate with Ansible basic (2/e)Automate with Ansible basic (2/e)
Automate with Ansible basic (2/e)Chu-Siang Lai
 
compiler fuzzer : prog fuzz介紹
compiler fuzzer : prog fuzz介紹compiler fuzzer : prog fuzz介紹
compiler fuzzer : prog fuzz介紹fdgkhdkgh tommy
 
網路組-Ubuntu介紹
網路組-Ubuntu介紹網路組-Ubuntu介紹
網路組-Ubuntu介紹maryqute520
 

Semelhante a Using Shell & Mastering Shell (20)

1, shell intro
1, shell intro1, shell intro
1, shell intro
 
使用Dsl改善软件设计
使用Dsl改善软件设计使用Dsl改善软件设计
使用Dsl改善软件设计
 
Linux必备知识与Unix基础文化
Linux必备知识与Unix基础文化Linux必备知识与Unix基础文化
Linux必备知识与Unix基础文化
 
scrapy+sphinx搭建搜索引擎
scrapy+sphinx搭建搜索引擎scrapy+sphinx搭建搜索引擎
scrapy+sphinx搭建搜索引擎
 
PHP 語法基礎與物件導向
PHP 語法基礎與物件導向PHP 語法基礎與物件導向
PHP 語法基礎與物件導向
 
02.python基础
02.python基础02.python基础
02.python基础
 
OpenWebSchool - 02 - PHP Part I
OpenWebSchool - 02 - PHP Part IOpenWebSchool - 02 - PHP Part I
OpenWebSchool - 02 - PHP Part I
 
Shell(bash) Scripting
Shell(bash) ScriptingShell(bash) Scripting
Shell(bash) Scripting
 
Linux Binary Exploitation - Stack buffer overflow
Linux Binary Exploitation - Stack buffer overflowLinux Binary Exploitation - Stack buffer overflow
Linux Binary Exploitation - Stack buffer overflow
 
Ubuntu
UbuntuUbuntu
Ubuntu
 
Linux基础
Linux基础Linux基础
Linux基础
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledge
 
shell script introduction
shell script introductionshell script introduction
shell script introduction
 
Binary exploitation - AIS3
Binary exploitation - AIS3Binary exploitation - AIS3
Binary exploitation - AIS3
 
Bash入门基础篇
Bash入门基础篇Bash入门基础篇
Bash入门基础篇
 
Introduce to Linux command line
Introduce to Linux command lineIntroduce to Linux command line
Introduce to Linux command line
 
Automate with Ansible basic (2/e)
Automate with Ansible basic (2/e)Automate with Ansible basic (2/e)
Automate with Ansible basic (2/e)
 
Execution
ExecutionExecution
Execution
 
compiler fuzzer : prog fuzz介紹
compiler fuzzer : prog fuzz介紹compiler fuzzer : prog fuzz介紹
compiler fuzzer : prog fuzz介紹
 
網路組-Ubuntu介紹
網路組-Ubuntu介紹網路組-Ubuntu介紹
網路組-Ubuntu介紹
 

Using Shell & Mastering Shell