鸟哥Linux(三)Shell Script && RE摘录

1.指令测试功能:test
a)测试文件
test -e
test -f
test -d
b)测试文件的属性
test -r filename
test -w filename
test -x filename:是否可执行
test -s filename:是否为非空的文件
c)比较两个文件
test file1 -nt file2: newer than
test file1 -ot file2: older than
test file1 -ef file2: wehther the same file
d)比较两个整数
test n1 -eq n2
test n1 -ne n2
test n1 -gt n2
test n1 -lt n2
test n1 -ge n2
test n1 -le n2
e)判断字符串
test -z str: 判读str是否为空字符串
test -n str: 判读str是否为非空字符串,-n可以省略
test str1=str2
test str1!=str2
f)多重条件判定: test -r filename -a -x filename
-a:同时成立
-o:任何一个成立
!:反向

2.[]代替test:[ "${HOME}" == "${MAIL}" ]

3.shell script的参数
sh spr_name op1, op2, op3 ...
     $0      $1  $2   $3

条件判断式:
if [ 条件判断 ]; then
   do sth1
elif
   do sth3
else
   do sth2
fi


case $var in
 "case1")
   do sth1
   ;;
 "case2")
   do sth2
   ;;
 "case3")
   do sth3
 *)
   do oth(do other thing)
   ;;
esac

while [ 条件 ]
do
      sth
done
如果条件成立,就一直循环

until [ 条件 ]
do
      sth
done
如果条件不成立,就一直循环

for ((初始值;限制值;执行步阶))
do
   sth
done

for var in con1 con2 con3...
do
   sth
done



function fname() {
     do sth
}
这里的函数也有参数的概念,也用$0, $1, $2表示



sh [-nvx] scripts.sh

-n:不执行,只检查语法
-v:执行sh前,先把sh的内容输出到屏幕上
-x:将使用到的sh内容显示到屏幕上(用于debug)



Shell Regular Expression:
^:既可以是表示开头也可以表示非([^a]不等于小写字幕a)
{}:在shell中使用要转义, grep -n 'o/{2/}', 列出有2个o出现的次数


扩展的grep(grep -E):
For Example:grep -v '^$' xxx.txt | grep -v '^#' = egrep -v '^$|^#' xxx.txt

+:
?:
|:
():egrep -n 'g(la|oo)d' xx.txt

printf命令:
printf '打印格式' 实际内容


sed -[nefr] [action]:
-n:使用silent模式,只有经过sed处理的行才会被显示出来
-e:直接在指令模式上进行sed的动作编辑
-f:
-r:sed的支持延伸正则表达式的语法,默认是基础正则表达式

[n1[,n2]]action

n1行-n2行,可以没有

action:
aSTR:add,STR会加在目前行的下一行
cSTR:replace,STR可以取代n1和n2之间的行
d:   delete
iSTR:add,STR会加在目前上的上一行
p:   print,将某个选择的数据打印到屏幕,
s:   替换,s通常搭配正则表达式,1,20s/old/new/g  (1-20行的所有old替换为new)

例子:去掉ifconfig得到结果的1-10行,ifconfig | sed '1,10d'


awk:用于处理一个整行,把一行分为几个字段来进行。
awk '条件类型1 {动作1} 条件类型2 {动作2}...' filename

每一行中的第一个字库用$1表示,以此类推,$0表示这一行

awk的内置变量:
NF 每行拥有的字段数
NR awk目前处理的是第几行
FS 目前的分隔符,默认是空格


文件比较(文本):
diff -[bBi] file1 file2
-b:忽略空格的差异
-B:忽略空白行的差异
-i:忽略大小写的差异

文件比较(二进制):
cmp [-s] file1 file2
-s:列出所有的不同点,因为默认只会输出第一个不同点




expr op1 op op2: 执行一个表达式, expr 3 /* 5, 输出15

eval:执行shell 命令

shift: 左移一个参数,$2变成$1
date的格式化输出: date '+%H:%M:%S %Y-%m-%d'

                             date +%s

base path: Print NAME with any leading directory components removed

dirname path: Print NAME with its trailing /component removed; if NAME contains

uname -a: 输出机器名的详细信息

echo $$: 输出本进程(bash)的ID

tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值