1. #!/bin/bash
2. ~/.bashrc, ~/.bash_profile
3. sh -n *.sh 测试shell脚本是否存在语法错误
4. sh -x *.sh(或者set -x) 进入跟踪方式,显示所执行的每一条命令,调试
5. set -x 开启调试, set +x 关闭调试
6. echo $? 返回值
7. echo -e 使显示的字符串中转义字符生效
8. read var / read -p "xxx" var 输入
9. myVar=${var:-"varvalue"} 变量var为空,则myVar="varvalue"
10. login shell(/etc/profile, ~/.bash_profile或~/.bash_login或~/.profile) / non-login shell(不会读配置,需要执行source生效)
11. ~/.bashrc non-login shell仅会读取.bashrc配置
12. $(), ` ` ==>在中间为子shell的起始与结束
13. ${} ==>在中间为命令区块的组合
14. ./*.sh ==>在子bash中执行shell脚本; source *.sh ==>在父bash中执行shell脚本
15. test指令; 如 test -e /usr/include/syslog.h && echo "exist" || echo "not exist"
-e 是否存在
-f 是否存在且为文件file
-d 是否存在且为目录directory
-b 是否存在且为block device
-c 是否存在且为character device
-S 是否存在且为Socket
-p 是否存在且为FIFO
-L 是否存在且为链接
-z 判断字符串是否为空串
//Page 453
16. [ ] 判断符号; --> 中括号两端需要有空格符来分割; 如[ -z "$HOME" ]; echo $?
17. 在bash中一个等号和两个等号结果是一致的,建议用两个等号做判断为佳
18. (1)中括号[]内的每个组件都需要有空格键来分割
(2)中括号[]内的变数,最好都以双引号括起来
(3)中括号[]内的常熟,最好都以单或双引号括起来
19. 参数
/path/to/script.sh opt1 opt2 opt3 opt4
$0 $1 $2 $3 $4
20. $# -- 代表后接的参数个数
$@ -- 代表[ "$1" "$2" "$3" "$4" ]
$* -- 代表[ "$1 $2 $3 $4" ] 默认以空格分割
21. if [ ]; then
#....
fi
if [ ]; then
#....
elif [ ]; then
#....
else
#....
fi
22. case $xxx in
"...")
;; <==每个结尾用两个连续的分号来处理
"")
;;
*)
;;
esac
23. function fname() {
程序段
}
24. function也拥有内建变量
functionname $1 $2 ...
25. while [ condition ]; do
#statements
done
until [ condition ]; do
#statements
done
for (( i = 0; i < 10; i++ )); do
#statements
done
for i in words; do
#statements
done
26. echo $(seq 1 10)
for i in $(seq 1 10); do
#statements
done
27. *, ?, [], [ - ], [^ ]
28. xxx > /dev/null 2>&1
29. RE 正则表达式
#///grep
^word ==> 待搜索的word在行首
word$ ==> 待搜索的word在行尾
. ==> 代表一定有一个任意字符
\ ==> 转义
* ==> 重复零个或者无穷个的前一个RE字符
[list]==> 字符集合,获取其中一个字符
[n1-n2]==>字符集合,范围内任意一个字符
[^list]==>反向选择
\{n,m\}==>连续n到m个的(前一个RE字符)
\{n\} ==>连续n个的前一个RE字符
\{n,\} ==>连续n个以上的前一个RE字符
#///egrep or grep -E
+ ==>重复一个或一个以上的前一个RE字符
? ==>零个或一个的前一个RE字符
| ==>用或(or)方式找出数个字符串
() ==>找出群组字符串 egrep -n 'g(la|oo)d' re.txt
()+ ==>多个重复群组的判别 echo 'AxyzxyzxyzxyzC' | egrep -n 'A(xyz)+C'
30. sed, awk
来源:oschina
链接:https://my.oschina.net/u/1581978/blog/603669