[] [[ ]] -n -z
-n:判断字符串是否非空,为空返回1,为false,非空返回0,为true。
-z:判断字符串是否为空,为空返回0,为true,非空返回1,为false。
[]: 内部变量需要加双引号
1 | pid="123" |
文件测试
1 | -e 文件名| 如果文件存在则为真 |
1 | -a :与 |
例:
1 | if test -e ./File1 -o -e ./File2 |
函数
1 |
|
输出:
1 | 这个函数会对输入的两个数字进行相加运算... |
参数
1 | $# 传递到脚本的参数个数 |
sed
最基本语句
增:a和i
1 | sed -e 4a\insert-a-line-after-line-4 distance.file |
删:d
1 | #删除第二行 |
改:c
1 | sed -e 1,2c\replace-line1-to-line2-with-this-line distance.file |
正则替换:s
1 |
组合命令:
1 | nl /etc/passwd | sed -n '/root/{s/bash/blueshell/;p;q}' |
字符串替换:s/ / /g
1 | cat distance.file | sed 's/source-string/replace-string/g' |
gawk
语法
1 | gawk options[可省略] program file |
grep
在当前目录递归(r)查找pattern字符串,忽略大小写(i),并输出pattern所在行号(n)
1 | grep -irn pattern |