目录

31. `ls -l` $(ls -l) 命令替换

32. > >> < <<   重定向

33. 管道  |

34. 算数计算  $[1+1]   $[ $height + 3]

35.  退出状态码 $?

36. exit

37. if ...; then fi  ( 还没有[ ]方括号的原始情况下  )

38. test命令  ==>  if [ condition ]

39. 数值比较 [ $val1 -eq 2 ]    只能比较整数    -eq -ge -gt

40. 字符串比较  [ $str1 \> $str2 ]     \>   \<   != =

41. 文件比较 [ -r file ]  -d -e -f -r -nt

42. ((  ))  [[  ]]

43. case


31. `ls -l` $(ls -l) 命令替换

`cmd` 和 $(cmd) 可以从 命令cmd的输出中提取信息,并将其赋给变量。


32. > >> < <<   重定向

command  >  file      // command的输出(覆盖)发送到文件file中

command  >>  file      // command的输出 追加到文件file的末尾(换行)

command  < inputfile      // 将文件inputfile中的内容输出给command

command  << EOF      // 内联输入重定向

data1

data2

...

EOF

cjh@cjh-PC:~/Shell_Program$ ./test4.sh
apple
banana
peach

33. 管道  |

command1 > MyFile

command2 < MyFile

等于

command1 | command2              // 省略中间文件


34. 算数计算  $[ 1+1 ]   $[ $height + 3 ]

用来替代expr的做法:$[  ]

再次强调:赋值的时候 "="前后不要有空格


35.  退出状态码 $?

Linux提供了一个专门的变量$?,来保存上个已执行命令的退出状态码。

cjh@cjh-PC:~/Shell_Program$ ./test4.sh
apple
banana
peach
cjh@cjh-PC:~/Shell_Program$ echo $?
0


36. exit

shell脚本文末指定 exit val, 返回自己的退出状态码。


37. if ...; then fi  ( 还没有[ ]方括号的原始情况下  )

bash shell的if语句会运行if后面的那个命令。如果该命令的退出状态码(参见35.)是0(该命令成功运行),位于then部分的命令就会被执行。如果该命令的退出状态码(参见35.)是非0,则运行else后的语句。

if ...; then

command

fi


if ...; then

command

else

command

fi


if ...; then

command

elif ...; then

command

elif ...; then

command

else

command

fi

cjh@cjh-PC:~/Shell_Program$ ./test39.sh
touch: 无法创建 '/opt/shit.txt': 权限不够
ls: 无法访问 '/home/Tom': 没有那个文件或目录
touch: 无法创建 './duck': 权限不够
We dont know who is here...
cjh@cjh-PC:~/Shell_Program$

38. test命令  ==>  if [ condition ]

if test condition

then

commands

fi

改进为

if [ condition ]; then

commands

fi

cjh@cjh-PC:~/Shell_Program$ ./test40.sh
The user exists and his name is : Mike.
The user  does not exist
cjh@cjh-PC:~/Shell_Program$

39. 数值比较 [ $val1 -eq 2 ]    只能比较整数    -eq -ge -gt

cjh@cjh-PC:~/Shell_Program$ ./test41a.sh
val1 equals to 2
val2 is smaller or equals to val3
val1 and val2 are not the same.
cjh@cjh-PC:~/Shell_Program$

40. 字符串比较  [ $str1 \> $str2 ]     \>   \<   != =

这里的字符串比较,是根据 ASCII值的顺序。

字符串比较大小 > < 前面要加转义符号

cjh@cjh-PC:~/Shell_Program$ ./test41b.sh
str1 and str2 are the same.
str1 and str2 are different.
str1: epidemic is bigger than str3: elephont.
str3 is not empty.
cjh@cjh-PC:~/Shell_Program$

41. 文件比较 [ -r file ]  -d -e -f -r -nt

cjh@cjh-PC:~/Shell_Program$ ./test41c.sh
The path CURRENT exists.
220320.log is here and can be written.
2022年 03月 21日 星期一 16:02:57 CST
2022年 03月 21日 星期一 16:03:12 CST
2022年 03月 21日 星期一 16:03:19 CST
2022年 03月 21日 星期一 16:09:41 CST
2022年 03月 29日 星期二 21:14:10 CST
file3 is very new.
cjh@cjh-PC:~/Shell_Program$

42. ((  ))  [[  ]]

(( )) 高级数学表达式,比较整数 -gt -eq。  不需要将双括号中表达式里的大于号转义

[[  ]]  针对字符串比较的高级特性  它提供了一个特性——模式匹配(pattern matching)  [] *


43. case

case variable in

pattern1 | pattern2)

commands1;;

pattern3)

commands2

        commands3;;

*)

default commands;;

esac

cjh@cjh-PC:~/Shell_Program$ ./test43.sh
The user is Jack or cjh.
The user is cjh
cjh@cjh-PC:~/Shell_Program$

Linux命令行与shell脚本编程之笔记(4)相关推荐

  1. Linux命令行与shell脚本编程之笔记(3)

    目录 24. /etc/passwd /etc/shadow /etc/group 25. 用户 () 26. 组 () 27. 安全性 () 28. fdisk fsck 29. dpkg 30. ...

  2. Linux命令行与shell脚本编程大全:第2版

    <Linux命令行与shell脚本编程大全:第2版> 基本信息 作者: (美)布卢姆(Blum,R.) 布雷斯纳汉(Bresnahan.C.) [作译者介绍] 译者: 武海峰 丛书名: 图 ...

  3. 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---34

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: 转载于:https://www.cnbl ...

  4. Linux命令行与shell脚本编程大全(第3版)

    作者:[美] 布鲁姆(Richard Blum),布雷斯纳汉(Christine Bresnahan) 著,门佳,武海峰 译 出版社:人民邮电出版社 品牌:iTuring 出版时间:2016-08-0 ...

  5. Linux_《Linux命令行与shell脚本编程大全》第十八章学习总结

    时间:2017年12月04日星期一 说明:本文部分内容均摘取自书籍<Linux命令行与shell脚本编程大全>,版权归原作者所有.<Linux命令行与shell脚本编程大全>( ...

  6. 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---02

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: 转载于:https://www.cnbl ...

  7. 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---20

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: 转载于:https://www.cnbl ...

  8. 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---43

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: 转载于:https://www.cnbl ...

  9. Linux_《Linux命令行与shell脚本编程大全》第二章学习总结

    时间:2017年04月05日 说明:本文部分内容均摘取自书籍<Linux命令行与shell脚本编程大全>,版权归原作者所有.<Linux命令行与shell脚本编程大全>(第三版 ...

最新文章

  1. 2021-05-09
  2. php+sqlrelay+mysql实现连接池及读写负载均衡
  3. ESP8266擦除flash
  4. centos7 安装图形界面、VNCserver
  5. SpringBoot 处理内置对象
  6. 数据结构之排序五:选择排序
  7. [AT2699]Flip and Rectangles
  8. 4. PHP递增/递减运算符
  9. Intel Hyperscan简介
  10. 编程工作怎么样手工问号
  11. 不吹不黑 | 聊聊为什么要用99%精度的数据回测
  12. 计数器代码php,php的计数器程序_php
  13. wordpress搜索ajax,基于wordpress的ajax写法详解
  14. WidsMob ImageConvert for Mac(图片格式转换器)
  15. 极域电子教室师生端连接不上怎么解决
  16. 1月好书推荐-8本值得读的好书,让你受益终生
  17. CAD2006 ----VBA(Hello World)
  18. 【强化学习】优势演员-评论员算法(Advantage Actor-Critic , A2C)求解倒立摆问题 + Pytorch代码实战
  19. 解决阿里云此手机号码绑定的账户数已达上限的方法
  20. java正常运行但javac报错

热门文章

  1. 高阶累积量四阶矩_概率论中「矩」(moment)的实际含义是什么,高阶矩表示数据的哪些状态?...
  2. 抖音短视频审核流程梳理
  3. mysql安装 防火墙,mysql8 参考手册--安装或卸载MySQL企业防火墙
  4. 安卓软件开发基础教学!写给1-3年安卓程序员的几点建议,跳槽薪资翻倍
  5. Verilog实现IIC协议读写EEPROM
  6. ghost 服务器系统,涨姿势:Ghost系统、纯净版系统、原版系统各是什么?
  7. org.springframework.web.HttpMediaTypeNotSupportedException Content type ‘ap
  8. 展会圆满收官 柏克利招商峰会业绩闪亮
  9. ipa文件包获取服务器地址,iOS获取App ipa包以及资源文件
  10. 关于 微软必应词典客户端 的案例分析