shell 学习笔记

《Linux命令行与shell脚本编程大全》笔记   wkss

其他:http://www.cnblogs.com/pengdonglin137/p/3528303.html

一、基本命令

二、基本结构

差缺补漏:

一、基本命令

1、stat命令

image

2、cat命令

  • -n  给所有行加上行号
  • -b  只给有文本的行加行号

image

3、tail

image

默认显示文件末尾10行。这里重点是 tail –f 的使用。

4、ps参数解释

参数风格:

image

  • 执行ps,默认只显示运行在当前控制台下的属于当前用户的进程。
  • ps -ef

image

image

  • ps -l

image

image

  • 树状显示   ps -efH或者ps --forest(GNU风格)

image

  • ps l  (注意与ps –l的区别不加’-’是BSD风格,加‘-’是UNIX风格)

image

image

image

5、top命令参数解释

image

image

image

image

6、mount命令

image

image

7、df命令

image

8、du命令

du默认显示当前目录下的所有文件、目录以及子目录的磁盘使用情况。

image

使用时我常用du –sh 路径名XX,可以显示文件或者文件夹XXX的大小,du –sh 显示当前所在目录的大小。

9、sort

image

-n表示把数字识别成数字而不是字符,-M 按月排序

将du和sort连起来使用:

du -s * | sort –nr

   1:  321764    flash
   2:  39216    pub
   3:  28888    slave
   4:  21984    image
   5:  68    scripts
   6:  24    CreatImage.sh
   7:  8    version_collate.sh
   8:  8    readme
   9:  4    moveversion.sh
 
注意:如果使用du –sh *| sort –nr ,当第一列有M和K结尾的数字出现时,排序有误。

10、shell提示符

image

image

image

11、设置和删除环境变量

export 用于设置全局环境变量,unset用于删除环境变量

12、数组

   1:  #!/bin/bash
   2:   
   3:  my=(1 2 3 "peng")
   4:   
   5:  echo ${my[1]}
   6:  echo ${my[2]}
   7:  echo ${my[3]}
   8:   
   9:  my[4]=4
  10:  echo ${my[4]}
  11:   
  12:  unset my[2]
  13:  echo ${my[2]}
  14:  echo "end"

运行:

   1:  pengdl@debian:~/test/shell$ ./sh10.sh 
   2:  2
   3:  3
   4:  peng
   5:  4
   6:   
   7:  end
   8:  pengdl@debian:~/test/shell$ 

13、echo –n 和 echo -e

   1:  pengdl@debian:~/test/shell$ echo -n "peng"
   2:  pengpengdl@debian:~/test/shell$ echo -n "XXXX"
   3:  XXXXpengdl@debian:~/test/shell$ echo  "XXXX
"
   4:  XXXX
   5:  pengdl@debian:~/test/shell$ echo -e "XXXX
"
   6:  XXXX
   7:   
   8:  pengdl@debian:~/test/shell$ echo -en "XXXX
"
   9:  XXXX
  10:  pengdl@debian:~/test/shell$ 

14、统计文本 wc

   1:   echo "peng dong lin 137" >1.txt 
   2:   wc < 1.txt 
   3:   1  4 18

image

15、内联输出重定向

image

16、查看退出状态码  $?

image

example:

   1:  pengdl@debian:~/test/shell$ jfjak
   2:  bash: jfjak: command not found
   3:  pengdl@debian:~/test/shell$ echo $?
   4:  127
   5:  pengdl@debian:~/test/shell$ 

二、基本结构

  • if

image

image

image

image

image

image

image

image

  • test命令

image

数值比较(注意: test不支持浮点数比较

image

字符串比较

image

image

字符串顺序

image

image

注意:

image

字符串大小

-n 如果非空,返回1;

-z 如果空, 返回1

image

image

文件比较

image

注意:

-d 如果是目录,返回1

-e 如果存在,返回1

复合条件测试

更多:http://www.cnblogs.com/pengdonglin137/p/3522757.html

image

image

if-then的高级特性

  • 双圆括号

image

除了test命令使用的标准数学运算符,下标列出了双圆括号命令会使用的其他运算符:

image

image

注:其中的大于号不需要转义。

  • 双方括号

image

image


case

image

for命令

  • 基本格式:

image

image

  • 转义

image

  • 引号

image

  • 从变量读取列表

image

image

  • 从命令读取值

image

image

  • 更该字段分隔符

image

image

  • 用通配符读取目录

image

注:为什么要用双引号将$file引起来?

image

  • 使用通配符读取目录2

image

image

C语言风格的for命令

image

  • 使用多个变量

image

while命令

 

原文地址:https://www.cnblogs.com/pengdonglin137/p/3527068.html