《Linux命令行与shell脚本编程大全》笔记 wkss
其他:http://www.cnblogs.com/pengdonglin137/p/3528303.html
差缺补漏:
一、基本命令
1、stat命令
2、cat命令
- -n 给所有行加上行号
- -b 只给有文本的行加行号
3、tail
默认显示文件末尾10行。这里重点是 tail –f 的使用。
4、ps参数解释
参数风格:
- 执行ps,默认只显示运行在当前控制台下的属于当前用户的进程。
- ps -ef
- ps -l
- 树状显示 ps -efH或者ps --forest(GNU风格)
- ps l (注意与ps –l的区别不加’-’是BSD风格,加‘-’是UNIX风格)
5、top命令参数解释
6、mount命令
7、df命令
8、du命令
du默认显示当前目录下的所有文件、目录以及子目录的磁盘使用情况。
使用时我常用du –sh 路径名XX,可以显示文件或者文件夹XXX的大小,du –sh 显示当前所在目录的大小。
9、sort
-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
10、shell提示符
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\n"
4: XXXX\n
5: pengdl@debian:~/test/shell$ echo -e "XXXX\n"
6: XXXX
7:
8: pengdl@debian:~/test/shell$ echo -en "XXXX\n"
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
15、内联输出重定向
16、查看退出状态码 $?
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
test命令
数值比较(注意: test不支持浮点数比较)
字符串比较
字符串顺序
注意:
字符串大小
-n 如果非空,返回1;
-z 如果空, 返回1
文件比较
注意:
-d 如果是目录,返回1
-e 如果存在,返回1
复合条件测试
更多:http://www.cnblogs.com/pengdonglin137/p/3522757.html
if-then的高级特性
- 双圆括号
除了test命令使用的标准数学运算符,下标列出了双圆括号命令会使用的其他运算符:
注:其中的大于号不需要转义。
- 双方括号
case
for命令
- 基本格式:
- 转义
- 引号
- 从变量读取列表
- 从命令读取值
- 更该字段分隔符
- 用通配符读取目录
注:为什么要用双引号将$file引起来?
- 使用通配符读取目录2
C语言风格的for命令
- 使用多个变量