1 / 36

第八章

第八章. Shell 基础. 第八章 shell 基础. 第八章 shell 基础 (2). 本章要点. 了解 shell 的意义与作用 掌握基本的 shell 操作. 8.1.1 shell 概述. shell 是系统用户界面,提供了用户与操作系统内核进行交互操作的接口. shell 是命令解释器,解释用户输入的命令,把它们送到内核去执行. shell 是脚本编程语言. AIX 默认用的 shell 是 ksh. 8.1.2 通配符. ? 通配符. * 通配符. [ ] 通配符. [ - ] 通配符.

mayten
Download Presentation

第八章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第八章 Shell 基础

  2. 第八章 shell基础

  3. 第八章 shell基础(2) 本章要点 了解shell 的意义与作用 掌握基本的shell 操作

  4. 8.1.1 shell概述 shell 是系统用户界面,提供了用户与操作系统内核进行交互操作的接口 shell是命令解释器,解释用户输入的命令,把它们送到内核去执行 shell 是脚本编程语言 AIX默认用的shell是ksh

  5. 8.1.2 通配符 ? 通配符 * 通配符 [ ] 通配符 [ - ] 通配符 ! 通配符

  6. 示例 ?通配符,通配任意某个字符 $ ls doc few net new tmp nod $ ls ne? net new $ ls ?e? few net new ?e?通配中间字符为e的共三个字符的字符串

  7. 示例 *通配符,通配任意字符串 $ ls doc few net new tmp nod $ ls n* net new nod $ ls *w few new *w通配以w结束的任意字符串

  8. 示例 [ ] 通配符,通配方括号中列出的字符 $ ls doc few net new tmp beet $ ls [fghbdn]e[tw] net new beet [fghbdn]通配f、g、h、b、d、n其中的一个字符

  9. 示例 [ - ] 通配符,通配方括号之间所列出的字符 $ ls test1 test2 test3 john2 limhai $ ls [a-z]*[2-5] test2 test3 john2 [a-z]表示在a到z之间的26个字母任意一个 [2-5]表示在2到5之间的4个数字任意一个

  10. 示例 ! 通配符,不匹配某个字符或字符串 $ ls test1 test2 test3 john2 limhai $ ls [!j]*[2-5] test2 test3 [!j]表示除了j字母外的任意字符

  11. 8.2.1 重定向 文件描述符 输入重定向,符号“ < ” 输出重定向,符号“ > ”和“ >> ” 错误重定向,符号“ 2> ”和“ 2>> ” 复合重定向

  12. 文件描述符 shell 解释命令,涉及如下三个文件描述符: • 标准输入(stdin): < 0 • 标准输出(stdout):> 1 • 标准错误(stderr):2> 2

  13. 输入重定向 普通发送邮件过程: $ mail limhai Subject: Meeting Hello. <ctrl+d> Cc: <enter> $ 把file文件内容重定向输入为要发送的邮件内容: $ mail limhai <file

  14. 输出重定向 示例把ls命令的输出重定向到 ls.out文件: $ ls test1 john2 limhai $ ls >ls.out $ cat ls.out test1 john2 limhai

  15. 输出重定向(2) 示例把who命令的输出重定向追加(append)到 ls.out文件: $ cat ls.out test1 john2 limhai $ who root lft0 Jun 09 16:20 $ who >>ls.out $ cat ls.out test1 john2 limhai root lft0 Jun 09 16:20

  16. 输出重定向(3) 使用输出重定向,cat命令创建newfile文件: $ ls test1 john2 limhai $ cat >newfile This is line 1 of the file <ctrl+d> $ ls test1 john2 limhai newfile

  17. 错误重定向 默认的标准错误输出: $ cat newfile oldfile This is line 1 of the file cat: 0652-050 Cannot open oldfile. 将标准错误重定向输出到errfile文件: $ cat newfile oldfile 2>errfile This is line 1 of the file $ cat errfile cat: 0652-050 Cannot open oldfile.

  18. 错误重定向(2) 将标准输出重定向outfile文件,并把标准错误重定向输出到errfile文件: $ ls -l >outfile 2>errfile 将标准输出和准错误输出都重定向到allfile文件: $ ls -l >allfile 2>&1

  19. 错误重定向(3) 示例,比较下面两个错误重定向: $ cat newfile oldfile 2>&1 >outfile cat: 0652-050 Cannot open oldfile. $ cat outfile This is line 1 of the file $ cat newfile oldfile >outfile 2>&1 $ cat outfile This is line 1 of the file cat: 0652-050 Cannot open oldfile.

  20. 复合重定向 复合重定向的格式: $ 命令 >outfile 2>errfile <infile 或 $ 命令 >>outfile 2>>errfile <infile

  21. 复合重定向(2) 示例: $ ls file mbox tmp $ cat >outfile 2>errfile <oldfile $ cat outfile $ cat errfile cat: cannot open oldfile

  22. 8.2.2 常用shell操作 管道,符号“ | ” 过滤,命令“grep ” 分离输出,命令“ tee ” 多重命令,符号“ ; ” 命令太长,符号“ \ ”和“ > ”

  23. 管道 管道(pipe)的目的是利用简单的命令组合来完成复杂的操作 管道把前面命令的输出作为后面命令的输入 管道的shell 操作符号是 |

  24. 管道(2) 示例1,统计当前在线用户的人数: $ who|wc -l 2 上面的管道示例,相当于如下过程: $ who >tempfile $ wc -l tempfile 2 tempfile $ rm tempfile

  25. 管道(3) 示例2,统计当前目录中按所占的盘区数目,从大到小排列前20个文件及其所占的盘区数目: $ du -a|sort –nr|head -20 示例3,统计当前目录中的文件数目: $ ls -l|wc -l

  26. 过滤 grep命令将把符合要求的内容过滤出来 示例,将ls命令输出结果用grep命令把以d字符开头的行过滤出来,作为wc命令的输入: $ ls -l |grep“^d” |wc -l

  27. 分离输出 tee命令读标准输入,将输出结果同时输出到两个地方:标准输出和指定文件 示例,将ls命令输出结果用tee命令,一方面输出到/tmp/ls.out文件,另一方面输出给wc命令,作为wc命令的输入: $ ls |tee /tmp/ls.out|wc -l

  28. 分离输出(2) $ ls |tee /tmp/ls.out |wc-l file file2 tee file file2 file file2 ls wc -l /tmp/ls.out file file2

  29. 多重命令 命令行要连续执行多个命令,用“; ”分开 示例,先执行ls命令,再执行exit命令: $ ls -R >outfile;exit 上面的管道示例,相当于如下过程: $ ls -R >outfile $ exit

  30. 命令太长 输入的命令太长,一行输不完,用下面方法解决: 在行末键入“\”,回车后,在第二行将自动出现PS2的提示符“>”,则可以在其后面继续输完剩下的命令,如下所示: $ cat /home/mydir/mysubdir/mydata \ > /home/yourdir/yoursubdir/yourdata

  31. 8.2.3 测试题 1、下面描述哪些是正确的?(多选) • 多重命令,用符号“ ; ”分开 • 输出重定向符号为“ > ”或“ >> ” • AIX默认用的shell是ksh • 管道(pipe)把前面命令的输出作为后面命令的输入

  32. 测试题 2 2、哪个是用以追加输出重定向的符号? • >> • > • \ • <

  33. 测试题 3 3、对通配符描述正确的有哪些?(多选) • ?g?通配中间字符为g的共三个字符的字符串 • *s 通配以s结束的任意字符串 • [3-6]表示在3到6之间的4个数字任意一个 • [!k]表示除了k字母外的任意字符

  34. 测试题 4 4、如下一个文件,如何用cat命令分别做到如下要求: 1)、把file1的文件内容定向到filea 文件,把错误输出定向到fileb 文件? 2)、把file1的文件内容和错误输出都定向到filea 文件? 3)、只是把file1的文件内容定向到filea文件,而且错误的输出不显示到屏幕,自动扔掉 $ ls file1

  35. 测试题 5 5、为了安全的需要,现需要将如下目录下的第二个字符为g的文件的权限都改为600,请问如何通过使用通配符快捷实现这个操作? $ pwd /home/limhai/security $ ls again age bag good lg tgz

  36. 测试题答案 1、A、B、C、D 2、A 3、A、B、 C、D 4、1)、cat file1 > filea 2> fileb 2)、cat file1 > filea 2> &1 3)、cat file1 > filea 2> /dev/null 5、chmod 600 ?g*

More Related