shift命令是Linux中的一个内置命令,用于移动命令行参数。它可以将命令行参数向左移动一个位置,即将$2号参数移动到$1号参数的位置,依次类推。
[root@ansible_nfs 7-3]# cat run.sh
#!/bin/bash
while (( $# != 0 ))
do
echo "第一个参数为:$1,参数的个数是:$#,所有的位置变量内容是:$@"
#删除第一个位置变量,将所有的位置变量左移一个位置
shift
done
[root@ansible_nfs 7-3]# bash run.sh gao jia liu jin yi tong
第一个参数为:gao,参数的个数是:6,所有的位置变量内容是:gao jia liu jin yi tong
第一个参数为:jia,参数的个数是:5,所有的位置变量内容是:jia liu jin yi tong
第一个参数为:liu,参数的个数是:4,所有的位置变量内容是:liu jin yi tong
第一个参数为:jin,参数的个数是:3,所有的位置变量内容是:jin yi tong
第一个参数为:yi,参数的个数是:2,所有的位置变量内容是:yi tong
第一个参数为:tong,参数的个数是:1,所有的位置变量内容是:tong
shell里的间接引用,执行了2遍
[root@ansible_nfs 7-3]# a=b
[root@ansible_nfs 7-3]# b=10
[root@ansible_nfs 7-3]# echo $a
b
[root@ansible_nfs 7-3]# echo ${!a}
10
eval 让bash解释2次,这样可以执行2次命令
[root@ansible_nfs 7-3]# cat a.txt
san
[root@ansible_nfs 7-3]# a="cat a.txt"
[root@ansible_nfs 7-3]# echo $a
cat a.txt
[root@ansible_nfs 7-3]# eval echo $a
cat a.txt
[root@ansible_nfs 7-3]# eval $a
san
bash自带的处理字符串
[root@ansible_nfs 7-3]# a='tiankai,gaosuo'
[root@ansible_nfs 7-3]# echo $a
tiankai,gaosuo
[root@ansible_nfs 7-3]# echo ${a:3} #从第3个开始,取到结束,下标从0开始
nkai,gaosuo
[root@ansible_nfs 7-3]# echo ${a:3:2} ##从第3个开始,取2个字符
nk
[root@ansible_nfs 7-3]# echo ${a:-2}
tiankai,gaosuo
[root@ansible_nfs 7-3]# echo ${a:-1}
tiankai,gaosuo
[root@ansible_nfs 7-3]# echo ${a: -1}
o
[root@ansible_nfs 7-3]# echo ${a: -2}
uo
[root@ansible_nfs 7-3]# echo ${a#*,}
gaosuo
[root@ansible_nfs 7-3]# echo ${a%,*}
tiankai
[root@ansible_nfs 7-3]# a="tian,gao,liu,jia,liu"
[root@ansible_nfs 7-3]# echo ${a/liu/wu}
tian,gao,wu,jia,liu
[root@ansible_nfs 7-3]# echo ${a//liu/wu}
tian,gao,wu,jia,wu
sort 默认是根据第1个字母的ASCII的大小进行升序排列,如果第1个字母一样,就根据第2个字母进行排序。
-n 让字符串识别成数值 number
-r 降序
-k 指定排序的列(字段)
-t 指定分隔符(默认是空白)
uniq 唯一 unique 去重
-c 统计重复出现的次数 count
-u 打印不重复的行
-d 打印出重复的行-c, --count
prefix lines by the number of occurrences-d, --repeated
only print duplicate lines, one for each group-u, --unique
only print unique lines
[root@ansible_nfs 7-1]# cat number.txt
1
2
1
2
3
5
6
4
4
[root@ansible_nfs 7-1]# cat number.txt |sort
1
1
2
2
3
4
4
5
6
[root@ansible_nfs 7-1]# cat number.txt |sort|uniq
1
2
3
4
5
6
[root@ansible_nfs 7-1]# cat number.txt |sort|uniq -c
2 1
2 2
1 3
2 4
1 5
1 6
[root@ansible_nfs 7-1]# cat number.txt |sort|uniq -u
3
5
6
[root@ansible_nfs 7-1]# cat number.txt |sort|uniq -d
1
2
4
[root@ansible_nfs 7-1]# cat /etc/passwd|sort -t ":" -k 3 -nr
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
chenyamin:x:1000:1000::/home/chenyamin:/bin/bash
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
zabbix:x:997:995:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
gitlab-www:x:996:993::/var/opt/gitlab/nginx:/bin/false
git:x:995:992::/var/opt/gitlab:/bin/sh
gitlab-redis:x:994:991::/var/opt/gitlab/redis:/bin/false
gitlab-psql:x:993:990::/var/opt/gitlab/postgresql:/bin/sh
gitlab-prometheus:x:992:989::/var/opt/gitlab/prometheus:/bin/sh
====
[root@ansible_nfs 7-1]# cat access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -6
[root@ansible_nfs 7-1]# cat /etc/passwd |sort -t ":" -k 3 -nr|awk -F ":" '{print $1,$3}'
nfsnobody 65534
chenyamin 1000
polkitd 999
chrony 998
zabbix 997
gitlab-www 996
git 995
gitlab-redis 994
gitlab-psql 993
gitlab-prometheus 992
mysqlrouter 991
systemd-network 192
nobody 99
postfix 89
dbus 81
sshd 74
rpc 32
rpcuser 29
ftp 14
games 12
operator 11
mail 8
halt 7
shutdown 6
sync 5
lp 4
adm 3
daemon 2
bin 1
root 0
[root@ansible_nfs 7-3]# cat /etc/passwd |awk -F ":" '{print $1,$3}'|sort -k 2 -nr
nfsnobody 65534
liu 1001
polkitd 999
chrony 998
zabbix 997
gitlab-www 996
git 995
gitlab-redis 994
gitlab-psql 993
gitlab-prometheus 992
mysqlrouter 991
systemd-network 192
nobody 99
postfix 89
dbus 81
sshd 74
rpc 32
rpcuser 29
ftp 14
games 12
operator 11
mail 8
halt 7
shutdown 6
sync 5
lp 4
adm 3
daemon 2
bin 1
root 0