写在前面:
博客书写牢记5W1H法则:What,Why,When,Where,Who,How。
本篇命令列表:
☉ wc
☉ cut
☉ awk
☉ sort
☉ uniq
☉ diff
☉ patch
wc
print newline, word, and byte counts for each file
wc [OPTION]... [FILE]...
-c, --bytes
-m, --chars
-l, --lines
-L, --max-line-length
-w, --words
cut
remove sections from each line of files
cut OPTION... [FILE]...
-d, --delimiter=DELIM
-f, --fields=LIST
-s, --only-delimited
注意:-f后跟的选项参数格式可以是:N N- N-M -M N,M格式
实例:
[root@localhost ~]# cut -d: -f -3 /etc/passwdroot:x:0bin:x:1daemon:x:2adm:x:3.....省略输出.....
awk
pattern scanning and processing language:模式扫描和加工语言
-F fs, --field-separator fs:指定分隔符
常用格式:
awk -F CHAR '/pattern/{print $1, $2, ...}' FILE...
实例:
#使用awk取出/etc/passwd文件中root用户的UID
[root@localhost shell]# awk -F: '/^root\>/{print $3}' /etc/passwd
sort
sort lines of text files
sort [OPTION]... [FILE]...
-t, --field-separator=SEP:设置列分隔符
-k, --key=KEYDEF:设置用作排序的字段
-f, --ignore-case
-n, --numeric-sort:根据数值大小排序
-r, --reverse:逆序
-u, --unique
-c, --check, --check=diagnose-first:检查是否按顺序排列
-b, --ignore-leading-blanks
-d, --dictionary-order:只考虑空白符和字母数字
-i, --ignore-nonprinting
注意:环境变量可能会影响排序,设置LC_ALL=C。
实例:
#将passwd以UID行以数字大小由大到小排序
[root@localhost ~]# sort -n -t: -r -k3 /etc/passwd | cut -d: -f -3nfsnobody:x:65534storm:x:1003fred:x:1002mageedu:x:1000systemd-bus-proxy:x:999systemd-network:x:998polkitd:x:997.....省略输出.....
uniq
report or omit repeated lines
-c, --count
-d, --repeated
-i, --ignore-case
-u, --unique
diff
compare files line by line
diff [OPTION]... FILES
格式为;
diff 选项 旧文件 新文件
-r, --recursive:递归比较子目录下文件
-i, --ignore-case
-u, -U NUM, --unified[=NUM]:使用unified机制,即如何显示要修改的行上下文,默认为3行;
实例:
#复制/etc/fstab为fstab1和fstab2并随意修改fstab2文件的其中两行内容[root@localhost ~]# cp /etc/fstab fstab1[root@localhost ~]# cp /etc/fstab fstab2[root@localhost ~]# vim fstab2[root@localhost ~]# diff fstab1 fstab2 10c10< UUID=4c719b2c-10bf-4a6d-adf1-8edcfbb070c0 /boot xfs defaults 0 0---> new12c12< UUID=6e2555f7-cca3-4904-a009-0e74811c3a21 swap swap defaults 0 0---> new
#输出的意义如下:
10c10:第一个10表示旧文件file1,第二个10表示新文件file2的第10行,c表示替换
< 表示剔除的行的内容
> 表示插入的行的内容
patch
apply a diff file to an original
-i patchfile or --input=patchfile:指定补丁文件路径
-R or --reverse:还原
实例:
#以上例中的两文件为例,将文件fstab1的内容更新为fstab2的内容,即更新到新版[root@localhost ~]# diff fstab1 fstab2 > fstab.patch[root@localhost ~]# patch -i fstab.patch fstab1 patching file fstab1[root@localhost ~]# cat fstab1 ## /etc/fstab# Created by anaconda on Thu Mar 3 18:40:19 2016## Accessible filesystems, by reference, are maintained under '/dev/disk'# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#UUID=e0d03544-5831-46cd-85c8-10b635fe8d36 / xfs defaults 0 0newUUID=6b6b6d40-2fbb-48d7-b4be-f34273b66bbb /usr xfs defaults 0 0new#还原fstab1原来的内容,即恢复到旧版本[root@localhost ~]# patch -R -i fstab.patch fstab1 patching file fstab1[root@localhost ~]# cat fstab1## /etc/fstab# Created by anaconda on Thu Mar 3 18:40:19 2016## Accessible filesystems, by reference, are maintained under '/dev/disk'# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#UUID=e0d03544-5831-46cd-85c8-10b635fe8d36 / xfs defaults 0 0UUID=4c719b2c-10bf-4a6d-adf1-8edcfbb070c0 /boot xfs defaults 0 0UUID=6b6b6d40-2fbb-48d7-b4be-f34273b66bbb /usr xfs defaults 0 0UUID=6e2555f7-cca3-4904-a009-0e74811c3a21 swap swap defaults 0 0
bash脚本之算数运算:
算数运算符:
+,-
*,/
%:求余
**:次方,幂运算
算数运算格式;
(1)let VAR=算数表达式
(2)VAR=$[算数表达式]
(3)VAR=$((算数表达式))
(4)VAR=$(expr $ARG1 $OP $ARG2):其中$OP表示运算符
注意:有些情况下,“*”需要转义,如第4中运算方式。
实例:
1、写一个脚本,计算/etc/passwd文件后3行用户的UID和。
[root@localhost shell]# cat sum.sh #!/bin/bash# get file /etc/passwd last 3 users' UID,then get SUM.tempfile=`mktemp $0.XXX.tmp`tail -n3 /etc/passwd | cut -d: -f 3 > $tempfileUID1=`head -n1 $tempfile`UID2=`head -n2 $tempfile | tail -n1`UID3=`tail -n1 $tempfile`SUM=$[$UID1 + $UID2 + $UID3]echo $SUMrm -rf $tempfile &> /dev/null