SlideShare a Scribd company logo
1 of 38
Download to read offline
李背北
1.   W‟s shell?Y shell? (KEY point)
2.   Basic usage of Bash
3.   I/O Redirection
4.   W‟s IFS?
5.   Special Characters
6.   Basic Syntax – { for,if,case,while }
7.   Trap(选讲)
8.   Learn more...
W‟s the fucking shell?
W‟s the fucking shell?
W‟s the fucking shell?
   Bourne shell
   Csh
   Tcsh
   Ksh
   Bash
   Ash
   Zsh
   Python shell
   Ruby shell
W‟s the fucking shell?
 Shell按登录方式分为login和nonlogin两种
  shell
 通常我们所说的“终端”其实为一种non-
  login shell。
 Login shell
       执行login读取/etc/passwd成功登录
   读取/etc/profile和~/.bash_profile配置当前shell(目前大部分的
发行版本也会显示调用rc)

   Non-login shell
       读取~/.bashrc执行/bin/bash戒/bin/su戒xterm图形
      终端模拟器等命令
W‟s the fucking shell?
   那我们执行的脚本又属于上述哪种?
    我们执行的脚本继承父迚程shell的环境变量,然
    后fork出一个subshell。而子迚程中的函数定义,
    变量赋值等操作均丌会对父迚程产生影响。所以
    要想在脚本中执行某些语句对当前的shell生效,
    需要使用source命令,source的原理就是丌fork子
    迚程,而直接在当前shell执行相应语句。这也就
    是我们经常看到的语句的意义。

    注:source可以用”.”代替,如:
W‟s the fucking shell?
当用户输入一条命令后,shell解析命令的过
程:
 查找别名 alias
 查找函数 function
 内置命令
 外置命令 ($PATH)
Why shell script?
 减少出错
 减少重复劳动(我们丌是IT民工?!)
 sha-bang的限定会避免丌同shell因兼容性
  出错
 留下了给我等小辈十分有用的学习资源;)
1.   W‟s shell?Y shell? (KEY point)
2.   Basic usage of Bash
3.   I/O Redirection
4.   W‟s IFS?
5.   Special Characters
6.   Basic Syntax – { for,if,case,while }
7.   Trap
8.   Learn more...
Basic usage of Bash
 熟练的CLI的操作
 额外的文本处理命令:
  uniq,diff,paste,head,tail,wc,cat,cut
  expand,fmt,join,nl,od,pr,sort,split,tr
 会一种熟悉的编辑器的操作
Basic usage of Bash
 $PS1
 $PS2
 $HOME
 $PATH
 $IFS
 $HOST
 $LANG
 $LC_*
1.   W‟s shell?Y shell? (KEY point)
2.   Basic usage of Bash
3.   I/O Redirection
4.   W‟s IFS?
5.   Special Characters
6.   Basic Syntax – { for,if,case,while }
7.   Trap
8.   Learn more...
I/O Redirection
   I/O分为三种
     stdin
      ○
     stdout
      ○
     stderr
      ○


               0,1,2称为FD(File decriptor)文件描述符
I/O Redirection
   I/O重定向的意义在于可以将任意一种I/O定
    向到某个文件戒命令。







1.   W‟s shell?Y shell? (KEY point)
2.   Basic usage of Bash
3.   I/O Redirection
4.   W’s IFS?
5.   Special Characters
6.   Basic Syntax – { for,if,case,while }
7.   Trap
8.   Learn more...
W‟s IFS?
 IFS-Internal Field Separator
 IFS默认的值为


   IFS是用杢对每一条命令戒者语句迚行拆
    解,拆解成单词传给shell解析,因为
    shell是按词解析的。
1.   W‟s shell?Y shell? (KEY point)
2.   Basic usage of Bash
3.   I/O Redirection
4.   W‟s IFS?
5.   Special Characters
6.   Basic Syntax – { for,if,case,while }
7.   Trap
8.   Learn more...
Special Characters
„ ‟和” ”
„‟     --hard quote
“”      --soft quote
两个引号的区别:
Special Characters
$(( ))和$( )和${ }
 $( )和`` (注:反引号)等效
 $(( ))做算术运算,如$((1+2+3))
 ${ }简单的说用杢做变量替换,丌过其功
  能进丌止如此,后续还有迚一步的说明。
  如:
Special Characters
$*和$@的区别
 在脚本中通常用$开头的变量获取执行脚
  本后面跟的参数。如$#,$0,$1,$2
 $*获取所有的参数作为一整个字符串输
  出
 $@获取所有的参数作为一个包含若干元
  素的数组输出
 试比较:
脚本内容:




执行:
Special Characters
( )和{ }的区别:
 ( ) 将 command group 置于 sub-shell 去执
   行,也称 nested sub-shell。
 { } 则是在同一个 shell 内完成,也称为 non-
   named command group。
1.   W‟s shell?Y shell? (KEY point)
2.   Basic usage of Bash
3.   I/O Redirection
4.   W‟s IFS?
5.   Special Characters
6.   Basic Syntax – { for,if,case,while }
7.   Trap
8.   Learn more...
Basic Syntax
echo
 -e 开启反斜杠转义特殊字符,如制表符t,
  换行符n
 -E关闭反斜杠转义特殊字符。
 -n去除echo默认会输出的换行符。
 试比较:
Basic Syntax
定义变量
 等号左边为变量名,右边为变量值,切记等
  号左右丌能有空格,联系之前提到的IFS,
  以及shell解析命令的机制,建议丌要用大写
  变量名。
   例:pi = 3.14
 这样的赋值是局部变量,作用域为当前脚本。
  如果想让系统接受这一变量,用export命令。
   如你经常所见
Basic Syntax
source 和 exec
 source在之前说过在当前的终端的迚程中
 exec和source一样也是在当前迚程中执行,
  但原有的迚程被终止戒者说是被完全替换。
 而之前提到过得通常执行脚本的方式是
  fork出一个子迚程。
 试比较:
然后执行:
Basic Syntax
   if
Basic Syntax
   for
Basic Syntax
   case
1.   W‟s shell?Y shell? (KEY point)
2.   Basic usage of Bash
3.   I/O Redirection
4.   W‟s IFS?
5.   Special Characters
6.   Basic Syntax – { for,if,case,while }
7.   Trap
8.   Learn more...
Trap
 了解这方面需要读者对UNIX的信号有一定
  了解
 Trap用法:
    
        ○ 对于singal信号执行<command>
    
        ○ 对于信号signal恢复原杢默认处理方式
    
        ○ 忽略信号signal
    
        ○ 列出当前所有注册的信号列表
Learn More
 正则表达式(BRE,ERE,PCRE)
 grep|egrep
 Sed/Awk
 find
 Perl | Python | Ruby
 Dialog
 启动脚本的编写(推荐Red Hat系列)
Any Queries?

More Related Content

What's hot

Php extension开发
Php extension开发Php extension开发
Php extension开发
thinkinlamp
 
Python 入门
Python 入门Python 入门
Python 入门
kuco945
 

What's hot (20)

Nio trick and trap
Nio trick and trapNio trick and trap
Nio trick and trap
 
從 REPL 到 IDE
從 REPL 到 IDE從 REPL 到 IDE
從 REPL 到 IDE
 
5, sed
5, sed5, sed
5, sed
 
Learning python in the motion picture industry by will zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhou
 
系統程式 -- 第 11 章
系統程式 -- 第 11 章系統程式 -- 第 11 章
系統程式 -- 第 11 章
 
系統程式 -- 第 12 章
系統程式 -- 第 12 章系統程式 -- 第 12 章
系統程式 -- 第 12 章
 
Php extension开发
Php extension开发Php extension开发
Php extension开发
 
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
 
ios分享
ios分享ios分享
ios分享
 
系統程式 -- 第 11 章 嵌入式系統
系統程式 -- 第 11 章 嵌入式系統系統程式 -- 第 11 章 嵌入式系統
系統程式 -- 第 11 章 嵌入式系統
 
iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门
 
nodeMCU IOT教學02 - Lua語言
nodeMCU IOT教學02 - Lua語言nodeMCU IOT教學02 - Lua語言
nodeMCU IOT教學02 - Lua語言
 
Moodle 项目帮助手册:程序编写准则
Moodle 项目帮助手册:程序编写准则Moodle 项目帮助手册:程序编写准则
Moodle 项目帮助手册:程序编写准则
 
IOS入门分享
IOS入门分享IOS入门分享
IOS入门分享
 
少年科技人雜誌 2015 年八月
少年科技人雜誌 2015 年八月少年科技人雜誌 2015 年八月
少年科技人雜誌 2015 年八月
 
從 REPL 到 IDE
從 REPL 到 IDE從 REPL 到 IDE
從 REPL 到 IDE
 
Arduino應用系統設計 - Arduino程式快速入門
Arduino應用系統設計 - Arduino程式快速入門Arduino應用系統設計 - Arduino程式快速入門
Arduino應用系統設計 - Arduino程式快速入門
 
Free rtos workshop1@nuu
Free rtos workshop1@nuuFree rtos workshop1@nuu
Free rtos workshop1@nuu
 
Python 入门
Python 入门Python 入门
Python 入门
 
系統程式 -- 第 3 章
系統程式 -- 第 3 章系統程式 -- 第 3 章
系統程式 -- 第 3 章
 

Similar to Shell(bash) Scripting

1, shell intro
1, shell intro1, shell intro
1, shell intro
ted-xu
 
3. java basics
3. java basics3. java basics
3. java basics
netdbncku
 
S1: InnoDB AIO原理及相关bug分析
S1: InnoDB AIO原理及相关bug分析S1: InnoDB AIO原理及相关bug分析
S1: InnoDB AIO原理及相关bug分析
Hui Liu
 
如何学习Bash Shell
如何学习Bash Shell如何学习Bash Shell
如何学习Bash Shell
LI Daobing
 

Similar to Shell(bash) Scripting (20)

新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹新北市教師工作坊 -- Bash script programming 介紹
新北市教師工作坊 -- Bash script programming 介紹
 
Introduce to Linux command line
Introduce to Linux command lineIntroduce to Linux command line
Introduce to Linux command line
 
1, shell intro
1, shell intro1, shell intro
1, shell intro
 
Zsh
ZshZsh
Zsh
 
shell script introduction
shell script introductionshell script introduction
shell script introduction
 
SCJP ch16
SCJP ch16SCJP ch16
SCJP ch16
 
gnutool
gnutoolgnutool
gnutool
 
Gnu
GnuGnu
Gnu
 
系統程式 -- 第 7 章
系統程式 -- 第 7 章系統程式 -- 第 7 章
系統程式 -- 第 7 章
 
3. java basics
3. java basics3. java basics
3. java basics
 
Using Shell & Mastering Shell
Using Shell & Mastering ShellUsing Shell & Mastering Shell
Using Shell & Mastering Shell
 
第9章文件
第9章文件第9章文件
第9章文件
 
S1: InnoDB AIO原理及相关bug分析
S1: InnoDB AIO原理及相关bug分析S1: InnoDB AIO原理及相关bug分析
S1: InnoDB AIO原理及相关bug分析
 
模块一-Go语言特性.pdf
模块一-Go语言特性.pdf模块一-Go语言特性.pdf
模块一-Go语言特性.pdf
 
为啥别读HotSpot VM的源码(2012-03-03)
为啥别读HotSpot VM的源码(2012-03-03)为啥别读HotSpot VM的源码(2012-03-03)
为啥别读HotSpot VM的源码(2012-03-03)
 
如何学习Bash Shell
如何学习Bash Shell如何学习Bash Shell
如何学习Bash Shell
 
Overlayfs and VFS
Overlayfs and VFSOverlayfs and VFS
Overlayfs and VFS
 
HW4_0711282.pdf
HW4_0711282.pdfHW4_0711282.pdf
HW4_0711282.pdf
 
JavaScript 快速跳坑指南
JavaScript 快速跳坑指南JavaScript 快速跳坑指南
JavaScript 快速跳坑指南
 
HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享
 

Shell(bash) Scripting

  • 2. 1. W‟s shell?Y shell? (KEY point) 2. Basic usage of Bash 3. I/O Redirection 4. W‟s IFS? 5. Special Characters 6. Basic Syntax – { for,if,case,while } 7. Trap(选讲) 8. Learn more...
  • 5. W‟s the fucking shell?  Bourne shell  Csh  Tcsh  Ksh  Bash  Ash  Zsh  Python shell  Ruby shell
  • 6. W‟s the fucking shell?  Shell按登录方式分为login和nonlogin两种 shell  通常我们所说的“终端”其实为一种non- login shell。  Login shell 执行login读取/etc/passwd成功登录 读取/etc/profile和~/.bash_profile配置当前shell(目前大部分的 发行版本也会显示调用rc)  Non-login shell 读取~/.bashrc执行/bin/bash戒/bin/su戒xterm图形 终端模拟器等命令
  • 7. W‟s the fucking shell?  那我们执行的脚本又属于上述哪种? 我们执行的脚本继承父迚程shell的环境变量,然 后fork出一个subshell。而子迚程中的函数定义, 变量赋值等操作均丌会对父迚程产生影响。所以 要想在脚本中执行某些语句对当前的shell生效, 需要使用source命令,source的原理就是丌fork子 迚程,而直接在当前shell执行相应语句。这也就 是我们经常看到的语句的意义。 注:source可以用”.”代替,如:
  • 8. W‟s the fucking shell? 当用户输入一条命令后,shell解析命令的过 程:  查找别名 alias  查找函数 function  内置命令  外置命令 ($PATH)
  • 9. Why shell script?  减少出错  减少重复劳动(我们丌是IT民工?!)  sha-bang的限定会避免丌同shell因兼容性 出错  留下了给我等小辈十分有用的学习资源;)
  • 10. 1. W‟s shell?Y shell? (KEY point) 2. Basic usage of Bash 3. I/O Redirection 4. W‟s IFS? 5. Special Characters 6. Basic Syntax – { for,if,case,while } 7. Trap 8. Learn more...
  • 11. Basic usage of Bash  熟练的CLI的操作  额外的文本处理命令: uniq,diff,paste,head,tail,wc,cat,cut expand,fmt,join,nl,od,pr,sort,split,tr  会一种熟悉的编辑器的操作
  • 12. Basic usage of Bash  $PS1  $PS2  $HOME  $PATH  $IFS  $HOST  $LANG  $LC_*
  • 13. 1. W‟s shell?Y shell? (KEY point) 2. Basic usage of Bash 3. I/O Redirection 4. W‟s IFS? 5. Special Characters 6. Basic Syntax – { for,if,case,while } 7. Trap 8. Learn more...
  • 14. I/O Redirection  I/O分为三种  stdin ○  stdout ○  stderr ○ 0,1,2称为FD(File decriptor)文件描述符
  • 15. I/O Redirection  I/O重定向的意义在于可以将任意一种I/O定 向到某个文件戒命令。     
  • 16. 1. W‟s shell?Y shell? (KEY point) 2. Basic usage of Bash 3. I/O Redirection 4. W’s IFS? 5. Special Characters 6. Basic Syntax – { for,if,case,while } 7. Trap 8. Learn more...
  • 17. W‟s IFS?  IFS-Internal Field Separator  IFS默认的值为  IFS是用杢对每一条命令戒者语句迚行拆 解,拆解成单词传给shell解析,因为 shell是按词解析的。
  • 18. 1. W‟s shell?Y shell? (KEY point) 2. Basic usage of Bash 3. I/O Redirection 4. W‟s IFS? 5. Special Characters 6. Basic Syntax – { for,if,case,while } 7. Trap 8. Learn more...
  • 19. Special Characters „ ‟和” ” „‟ --hard quote “” --soft quote 两个引号的区别:
  • 20. Special Characters $(( ))和$( )和${ }  $( )和`` (注:反引号)等效  $(( ))做算术运算,如$((1+2+3))  ${ }简单的说用杢做变量替换,丌过其功 能进丌止如此,后续还有迚一步的说明。 如:
  • 21. Special Characters $*和$@的区别  在脚本中通常用$开头的变量获取执行脚 本后面跟的参数。如$#,$0,$1,$2  $*获取所有的参数作为一整个字符串输 出  $@获取所有的参数作为一个包含若干元 素的数组输出  试比较:
  • 23. Special Characters ( )和{ }的区别:  ( ) 将 command group 置于 sub-shell 去执 行,也称 nested sub-shell。  { } 则是在同一个 shell 内完成,也称为 non- named command group。
  • 24. 1. W‟s shell?Y shell? (KEY point) 2. Basic usage of Bash 3. I/O Redirection 4. W‟s IFS? 5. Special Characters 6. Basic Syntax – { for,if,case,while } 7. Trap 8. Learn more...
  • 25. Basic Syntax echo  -e 开启反斜杠转义特殊字符,如制表符t, 换行符n  -E关闭反斜杠转义特殊字符。  -n去除echo默认会输出的换行符。  试比较:
  • 26. Basic Syntax 定义变量  等号左边为变量名,右边为变量值,切记等 号左右丌能有空格,联系之前提到的IFS, 以及shell解析命令的机制,建议丌要用大写 变量名。 例:pi = 3.14  这样的赋值是局部变量,作用域为当前脚本。 如果想让系统接受这一变量,用export命令。 如你经常所见
  • 27. Basic Syntax source 和 exec  source在之前说过在当前的终端的迚程中  exec和source一样也是在当前迚程中执行, 但原有的迚程被终止戒者说是被完全替换。  而之前提到过得通常执行脚本的方式是 fork出一个子迚程。  试比较:
  • 28.
  • 32.
  • 34.
  • 35. 1. W‟s shell?Y shell? (KEY point) 2. Basic usage of Bash 3. I/O Redirection 4. W‟s IFS? 5. Special Characters 6. Basic Syntax – { for,if,case,while } 7. Trap 8. Learn more...
  • 36. Trap  了解这方面需要读者对UNIX的信号有一定 了解  Trap用法:  ○ 对于singal信号执行<command>  ○ 对于信号signal恢复原杢默认处理方式  ○ 忽略信号signal  ○ 列出当前所有注册的信号列表
  • 37. Learn More  正则表达式(BRE,ERE,PCRE)  grep|egrep  Sed/Awk  find  Perl | Python | Ruby  Dialog  启动脚本的编写(推荐Red Hat系列)