shell中常用的基礎命令
阿新 • • 發佈:2020-12-15
文章目錄
1、diff
輸出資訊: [num1,num2][a|c|d][num3,num4] num1,num2 ##第一個檔案中的行 a ##新增 c ##更改 d ##刪除 < ##第一個檔案中的內容 > ##第二個檔案中的內容 num3,num4 ##第二個檔案中的行 常用引數: -b ##忽略空格 -B ##忽略空行 -i ##忽略大小寫 -c ##顯示檔案所有內容並標示不同 -r ##對比目錄 -u ##合併輸出
[[email protected] mnt]# cat westos
hello westos
[[email protected] mnt]# cat westos1
hello westos
123
[[email protected] mnt]# diff westos westos1
1a2
> 123
[[email protected] mnt]# vim westos1
[[email protected] mnt]# diff westos westos1
1c1,2
< hello westos
---
> hello westos
> 123
[[email protected] mnt]# diff -b westos westos1
1a2
> 123
[[email protected] mnt]# diff westos westos1
0a1
>
[[email protected] mnt]# diff -B westos westos1
[[email protected] mnt]# diff westos westos1
1c1
< hello westos
---
> Hello westos
[[email protected] mnt] # diff -i westos westos1
[[email protected] mnt]# diff -c westos westos1
*** westos 2020-12-14 16:24:43.062000000 +0800
--- westos1 2020-12-14 16:43:06.580000000 +0800
***************
*** 1 ****
--- 1,3 ----
+
hello westos
+ 123
[[email protected] mnt]# mkdir westosdir westosdir1
[[email protected] mnt]# touch westosdir/westosfile
[[email protected] mnt]# diff -r westosdir westosdir1
Only in westosdir: westosfile
[[email protected] mnt]# diff -u westos westos1 > westos.path
[[email protected] mnt]# cat westos.path
--- westos 2020-12-14 16:24:43.062000000 +0800
+++ westos1 2020-12-14 16:43:06.580000000 +0800
@@ -1 +1,3 @@
+
hello westos
+123
2、patch
patch 原檔案 布丁檔案
-b ##備份原檔案
[[email protected] mnt]# diff -u westos westos1 > westos.path
[[email protected] mnt]# patch -b westos westos.path
patching file westos
[[email protected] mnt]# ls
westos westos1 westosdir westosdir1 westos.orig westos.path
[[email protected] mnt]# cat westos.orig
hello westos
[[email protected] mnt]# cat westos
hello westos
123
[[email protected] mnt]# cat westos1
hello westos
123
3、cut
-d : ##指定:為分隔符
-f ##指定顯示的列 5第五列| 3,5 3和5列|3-5 3到5列|5- 第五列以後|-5 到第五列
-c ##指定擷取的字元(數字用法同-f)
[[email protected] mnt]# cut -d : -f 1 passwd
sssd
flatpak
colord
gdm
rpcuser
gnome-initial-setup
sshd
avahi
rngd
tcpdump
westos
postfix
named
[[email protected] mnt]# cut -d : -f 1,4 passwd
sssd:978
flatpak:977
colord:976
gdm:42
rpcuser:29
gnome-initial-setup:975
sshd:74
avahi:70
rngd:974
tcpdump:72
westos:1000
postfix:89
named:25
[[email protected] mnt]# cut -d : -f 1-4 passwd
sssd:x:980:978
flatpak:x:979:977
colord:x:978:976
gdm:x:42:42
rpcuser:x:29:29
gnome-initial-setup:x:977:975
sshd:x:74:74
avahi:x:70:70
rngd:x:976:974
tcpdump:x:72:72
westos:x:1000:1000
postfix:x:89:89
named:x:25:25
[[email protected] mnt]# cut -d : -f 5- passwd
User for sssd:/:/sbin/nologin
User for flatpak system helper:/:/sbin/nologin
User for colord:/var/lib/colord:/sbin/nologin
:/var/lib/gdm:/sbin/nologin
RPC Service User:/var/lib/nfs:/sbin/nologin
:/run/gnome-initial-setup/:/sbin/nologin
Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
:/:/sbin/nologin
westos:/home/westos:/bin/bash
:/var/spool/postfix:/sbin/nologin
Named:/var/named:/bin/false
[[email protected] mnt]# cut -d : -f -2 passwd
sssd:x
flatpak:x
colord:x
gdm:x
rpcuser:x
gnome-initial-setup:x
sshd:x
avahi:x
rngd:x
tcpdump:x
westos:x
postfix:x
named:x
[[email protected] mnt]# cut -c 1-4 passwd
sssd
flat
colo
gdm:
rpcu
gnom
sshd
avah
rngd
tcpd
west
post
name
[[email protected] mnt]# grep bash -v /etc/passwd | cut -d : -f 1
bin
daemon
adm
lp
sync
shutdown
halt
mail
4、sort
-n ##純數字排序
-r ##倒敘
-u ##去掉重複
-o ##輸出到指定檔案
-t ##指定分隔符
-k ##指定排序的列
[[email protected] mnt]# sort -n westos
1
2
5
6
8
8
9
9
12
26
68
68
[[email protected] mnt]# sort -nr westos
68
68
26
12
9
9
8
8
6
5
2
1
[[email protected] mnt]# sort -nr westos -o westos1
[[email protected] mnt]# cat westos1
68
68
26
12
9
9
8
8
6
5
2
1
[[email protected] mnt]# sort -u westos
1
12
2
26
5
6
68
8
9
[[email protected] mnt]# sort -t : -k 2 westos
6:1
2:12
2:2
8:26
5:5
2:6
0:68
8:68
0:8
0:8
0:9
1:9
[[email protected] mnt]# sort -t : -k 1 westos
0:68
0:8
0:8
0:9
1:9
2:12
2:2
2:6
5:5
6:1
8:26
8:68
5、uniq
-c #合併重複並統計重複個數
-d #顯示重複的行
-u #顯示唯一的行
[[email protected] mnt]# uniq -c westos
1 1
1 2
1 5
1 6
1 8
1 9
1 12
1 26
2 68
1 8
1 9
[[email protected] mnt]# uniq -d westos
68
[[email protected] mnt]# uniq -u westos
1
2
5
6
8
9
12
26
8
9
[[email protected] mnt]# sort -n westos | uniq -c | sort -k 1 -nr | cut -d " " -f 8 | head -n 1
9
[[email protected] mnt]# sort -n westos | uniq -c | sort -nrt " " -k 7 | cut -d " " -f 8 | head -n 1
9
命令測試:
1.ifconfig 網絡卡 可以顯示此網絡卡的資訊
顯示資訊中包含此網絡卡使用的ip地址
請用命令過濾此ip並在輸出時只顯示ip其他資訊不顯示
[[email protected] mnt]# ifconfig enp1s0 | head -n 2 | tail -n 1 | cut -d " " -f 10
1.1.1.88
2.找出能登陸系統使用者中UID最大的使用者,並顯示其名稱
[[email protected] mnt]# grep bash /etc/passwd | sort -t : -k3 -n | tail -n 1 | cut -d : -f 1
3.當前主機為web伺服器,請抓取訪問web伺服器次數排在前5的ip地址
[[email protected] mnt]# cut -d " " -f 1 /etc/httpd/logs/access_log* | sort -n | uniq -c | sort -nr | head -n 5 | cut -d " " -f 7
[[email protected] mnt]# cut -d " " -f 1 /etc/httpd/logs/access_log* | sort -n | uniq -c | sort -nr | head -n 5 | sed 's/^ * //g' | cut -d " " -f 2
172.25.254.12
172.25.254.212
172.25.254.13
6、 tr
tr 'a-z' 'A-Z' ##小寫轉大寫
tr 'A-Z' 'a-z' ##大寫轉小寫
[[email protected] mnt]# ifconfig enp1s0 | head -2 | tail -1 | cut -d " " -f 10
1.1.1.88
[[email protected] mnt]# ifconfig enp1s0 | head -2 | tail -1 | cut -d " " -f 10 | tr '1' '*'
*.*.*.88
[[email protected] mnt]# ls
passwd westos
[[email protected] mnt]# ls | tr 'a-z' 'A-Z'
PASSWD
WESTOS
7、test、&&、 ||
&& 符合條件作動作
|| 不符合條件作動作
test = [] ##[] 就相當於test命令
"test $a = $b" = [ "$a" = "$b" ]
test數字對比
=
!=
-eq ##等於
-ne ##不等於
-le ##小於等於
-lt ##小於
-ge ##大於等於
-gt ##大於
test的條件關係
-a ##並且
-o ##或者
test對空的判定
-n ##nozero 判定內容不為空
-z ##zero 判定內容為空
[[email protected] ~]# a=1
[[email protected] ~]# b=1
[[email protected] ~]# test "$a" = "$b" && echo yes || echo no
yes
[[email protected] ~]# [ "$a" -eq "$b" ] && echo yes || echo no ##等於
[[email protected] ~]# [ "$a" -ne "$b" ] && echo yes || echo no ##不等於
[[email protected] ~]# [ "$a" -le "$b" ] && echo yes || echo no ##小於等於
[[email protected] ~]# [ "$a" -ge "$b" ] && echo yes || echo no ##大於等於
[[email protected] ~]# [ "$a" -lt "$b" ] && echo yes || echo no ##小於
[[email protected] ~]# [ "$a" -gt "$b" ] && echo yes || echo no ##大於
[[email protected] ~]# [ "$a" -gt "0" -a "$a" -lt "10" ] && echo yes || echo no ##並且
[[email protected] ~]# [ "$a" -le "0" -o "$a" -ge "1" ] && echo yes || echo no ##或者
[[email protected] ~]# [ "$a" -gt "0" ] && [ "$a" -lt "10" ] && echo yes || echo no
[[email protected] ~]# [ "$a" -gt "0" ] || [ "$a" -lt "1" ] && echo yes || echo no
[[email protected] ~]# [ -z "$c" ] && echo yes || echo no ##zero 判定內容為空
yes
[[email protected] ~]# [ -n "$c" ] && echo yes || echo no ##nozero 判定內容不為空
no
執行下列指令碼來判斷使用者型別
user_check.sh 使用者
- 使用者型別為
super user
system user
common user
[[email protected] ~]# vim test.sh
#!/bin/bash
[ -z "$1" ] && {
echo "Error: Please input username following script!"
exit
}
id $1 &> /dev/null || {
echo "user $1 is not exist !"
exit
}
USER_UID=$(id -u $1)
USER_SHELL=$( grep $1 /etc/passwd | cut -d : -f 7)
[ "$USER_UID" -eq "0" ] && {
echo $1 is supper user
exit
}
[ "$USER_UID" -lt "1000" ] && [ ! "$USER_SHELL" = "/bin/bash" ] && {
echo $1 is systemuser
exit
}
[ "$USER_UID" -ge "1000" ] && [ "$USER_SHELL" = "/bin/bash" ] && {
echo $1 is common user
exit
}
echo "unknow user type !!"
[[email protected] mnt]# sh test.sh linux
user linux is not exist !
[[email protected] mnt]# sh test.sh westos
westos is common user
[[email protected] mnt]# sh test.sh root
root is supper user
[[email protected] mnt]# sh test.sh lee
lee is common user
test對於檔案的判定
-ef ##檔案節點號是否一致(硬鏈)
-nt ##檔案1是不是比檔案2新
-ot ##檔案1是不是比檔案2老
-d ##目錄
-S ##套結字
-L ##軟連線
-e ##存在
-f ##普通檔案
-b ##快裝置
-c ##字元裝置
[[email protected] mnt]# [ "/mnt/westos" -ef "/mnt/westos1" ] && echo yes || echo no
no
[[email protected] mnt]# [ "/mnt/westos" -nt "/mnt/westos1" ] && echo yes || echo no
yes
[[email protected] mnt]# [ "/mnt/westos" -ot "/mnt/westos1" ] && echo yes || echo no
no
[[email protected] mnt]# [ -d "/mnt/" ] && echo yes || echo no
yes
[[email protected] mnt]# ln -s /mnt/westos file1
[[email protected] mnt]# ln /mnt/westos /mnt/file
[[email protected] mnt]# [ -L "/mnt/file1" ] && echo yes || echo no
yes
[[email protected] mnt]# [ -S "/mnt/file" ] && echo yes || echo no
no
[[email protected] mnt]# [ -e "/mnt/file" ] && echo yes || echo no
yes
[[email protected] mnt]# [ -c "/dev/vda" ] && echo yes || echo no
no
[[email protected] mnt]# [ -b "/dev/vda" ] && echo yes || echo no
yes
- 檢測
編寫指令碼完成以下條件
file_check.sh 在執行時
如果指令碼後未指定檢測檔案報錯“未指定檢測檔案,請指定”
如果指令碼後指定檔案不存在報錯“此檔案不存在”
當檔案存在時請檢測檔案型別並顯示到輸出中
[[email protected] mnt]# vim file_check.sh
#!/bin/bash
[ -z "$1" ] && {
echo Error: Please input file following script !!
exit
}
[ -e "$1" ] || {
echo Error: not found $1 !!
exit
}
[ -d "$1" ] && {
echo $1 is 目錄 !!
exit
}
[ -S "$1" ] && {
echo $1 is 套接字 !!
exit
}
[ -L "$1" ] && {
echo $1 is 軟連線 !!
exit
}
[ -f "$1" ] && {
echo $1 is 普通檔案 !!
exit
}
[ -b "$1" ] && {
echo $1 is 快裝置 !!
exit
}
[ -c "$1" ] && {
echo $1 is 字元裝置 !!
exit
}
[[email protected] mnt]# sh file_check.sh /mnt/westos
/mnt/westos is 普通檔案 !!
[[email protected] mnt]# sh file_check.sh /mnt/
/mnt/ is 目錄 !!