shell脚本常用基础命令1
shell脚本常用基础命令
一、 uniq指令
1.1参数c(对于顺序的合并重复并且统计个数)
[root@trade shell]# cat yan
123
4
1
2
3
7
81
13
81
13
81
81
123
4
123
[root@trade shell]# uniq -c yan
1 123
1 4
1 1
1 2
1 3
1 7
1 81
1 13
1 81
1 13
2 81
1 123
1 4
1 123
1.2uniq和sort配合使用(合并重复并统计重复个数)
[root@docker3 mnt]# sort -n yan | uniq -c
1 2
1 3
1 4
1 7
1 13
2 81
1 123
1.3参数d(显示重复的行)
[root@docker3 mnt]# sort -n yan | uniq -d
81
1.4参数u(显示唯一的行)
[root@docker3 mnt]# sort -n yan | uniq -u
1
2
3
4
7
13
123
1.5抓取出现数字最多的数字(不含重复出现最多数字一样)
[root@trade shell]# cat yan
123
4
1
2
3
7
81
13
81
13
81
81
123
4
123
[root@trade shell]# sort -n yan | uniq -c
1 1
1 2
1 3
2 4
1 7
2 13
4 81
3 123
[root@trade shell]# sort -n yan | uniq -c | sort -n -k 1
1 1
1 2
1 3
1 7
2 13
2 4
3 123
4 81
[root@trade shell]# sort -n yan | uniq -c | sort -n -k 1 | tail -n1
4 81
[root@trade shell]# sort -n yan | uniq -c | sort -n -k 1 | tail -n 1 | cut -d " " -f 8
81
1.6抓取网卡的ip
[kiosk@foundation38 Desktop]$ ifconfig wlan0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.31.177 netmask 255.255.255.0 broadcast 192.168.31.255
inet6 fe80::cd76:22a3:151a:9e8d prefixlen 64 scopeid 0x20<link>
ether b0:68:e6:99:ba:7d txqueuelen 1000 (Ethernet)
RX packets 34279 bytes 33490309 (31.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21281 bytes 5558502 (5.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[kiosk@foundation38 Desktop]$ ifconfig wlan0 | head -n 2
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.31.177 netmask 255.255.255.0 broadcast 192.168.31.255
[kiosk@foundation38 Desktop]$ ifconfig wlan0 | head -n 2 | tail -n 1
inet 192.168.31.177 netmask 255.255.255.0 broadcast 192.168.31.255
[kiosk@foundation38 Desktop]$ ifconfig wlan0 | head -n 2 | tail -n1 | cut -d " " -f 10
192.168.31.177
二、tr用法
tr用法
tr ‘a-z’ ‘A-Z’ 小写转换成大写
tr ‘A-Z’ ‘a-z’ 大写转换成小写
[root@docker3 mnt]# cat yan
hello
HELLO
[root@docker3 mnt]# tr 'l' 's' < yan
hesso
HELLO
[root@docker3 mnt]# tr 'a-z' 'A-Z' < yan
HELLO
HELLO
[root@docker3 mnt]# cat yan
hello
HELLO
三、shell脚本条件
&&条件成立则…(是)
||不成立则…(否)
[root@docker3 mnt]# ping -c1 -w1 172.25.254.3 &> /dev/null && echo 172.25.254.3 is up || echo 172.25.254.3 is down
172.25.254.3 is up
[root@docker3 mnt]# ping -c1 -w1 172.25.254.5 &> /dev/null && echo 172.25.254.5 is up || echo 172.25.254.5 is down
172.25.254.5 is down
四、test指令
4.1test用法
test是作比较的
test = [ ]
“test $a = $b” = [ " $a" = " $b" ]
test数字对比
=
!=
-eq 等于
-ne 不等于
-le 小于等于
-lt 小于
-ge 大于等于
-gt 大于
[root@docker3 mnt]# a=1
[root@docker3 mnt]# b=1
[root@docker3 mnt]# test "$a" = "$b" && echo yes || echo no
yes
[root@docker3 mnt]# a=2
[root@docker3 mnt]# test "$a" = "$b" && echo yes || echo no
no
[root@docker3 mnt]# [ "$a" = "$b" ] && ehco yes || echo no
no
[root@docker3 mnt]# [ ! "$a" = "$b" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ ! "$a" -eq "$b" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ ! "$a" -ne "$b" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ "$a" -le "$b" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
yes
4.2test的条件关系
-a 并且
-o 或者
[root@docker3 mnt]# [ "$a" -gt "0" -a "$a" -lt "10" ] && echo yes || echo no
yes
[root@docker3 mnt]# a=20
[root@docker3 mnt]# [ "$a" -gt "0" -a "$a" -lt "10" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "$a" -gt "0" -o "$a" -lt "10" ] && echo yes || echo no
yes
test对空的判定
-n nozero判定内容不为空
-z zero判定内容为空
[root@docker3 mnt]# [ -z "$c" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ -n "$c" ] && echo yes || echo no
no
五、脚本应用
5.1执行脚本来判断用户的类型
user_check.sh 用户
用户类型为
super user 0
system user 1-999
common user
$*表示脚本后面跟的所有字符串
[root@docker3 mnt]# sh user_check.sh
error : Please input username
[root@docker3 mnt]# sh user_check.sh yan
yan is common user
[root@docker3 mnt]# sh user_check.sh root
root is super user
[root@docker3 mnt]# sh user_check.sh out
ERROR: out is not exist
5.2执行脚本判断文件类型
test对于文件类型的判定
-ef 文件节点号是否一致(硬链)
-nt 文件1是否比文件2新
-ot 文件1是否比文件2老
-d 目录
-S 套接字
-L 软连接
-e 存在
-f 普通文件
-b 快设备
-c 字符设备
[root@docker3 mnt]# [ -e "/mnt" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ -d "/mnt" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ -S "/mnt" ] && echo yes || echo no
no
[root@docker3 mnt]# [ -L "/mnt" ] && echo yes || echo no
no
[root@docker3 mnt]# [ -f "/mnt" ] && echo yes || echo no
no
[root@docker3 mnt]# [ -c "/mnt" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "/mnt/yan" -ef "/mnt/yan1" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "/mnt/yan" -ef "/mnt/yan2" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ "/mnt/yan" -ot "/mnt/file" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "/mnt/yan" -nt "/mnt/file" ] && echo yes || echo no
yes
执行脚本
file_check.sh在执行时
如果脚本后未指定检测文件报错“未检测文件,请指定”
如果脚本后指定文件不存在报错“此文件不存在”
当文件存在时请检测文件类型并显示到输出中
END