博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SHELL中常用的一些简单命令
阅读量:3961 次
发布时间:2019-05-24

本文共 7340 字,大约阅读时间需要 24 分钟。

SHELL中常用的一些简单命令

1.diff

用法:diff [options] files|directorys输出信息:[num1,num2][a|c|d][num3,num4]num1,num2	##第一个文件中的行a		##添加c		##更改d		##删除<		##第一个文件中的内容>		##第二个文件中的内容num3,num4	##第二个文件中的行常用参数:-b	##忽略空格-B	##忽略空行-i	##忽略大小写-c	##显示文件所有内容并标示不同-r	##对比目录-u	##合并输出
[root@localhost mnt]# cat westoshello westos[root@localhost mnt]# cat westos1hello westos123[root@localhost mnt]# diff westos westos11a2> 123[root@localhost mnt]# vim westos1[root@localhost mnt]# diff westos westos11c1,2< hello westos---> hello  westos> 123[root@localhost mnt]# diff -b westos westos11a2> 123[root@localhost mnt]# diff -B westos westos1[root@localhost mnt]# diff westos westos11c1< hello westos---> Hello westos[root@localhost mnt]# diff -i westos westos1[root@localhost 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[root@localhost mnt]# mkdir westosdir westosdir1[root@localhost mnt]# touch westosdir/westosfile[root@localhost mnt]# diff -r westosdir westosdir1Only in westosdir: westosfile[root@localhost mnt]# diff -u westos westos1 > westos.path[root@localhost 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	##备份原文件dnf install patch.x86_64 -y[root@localhost mnt]# diff -u westos westos1 > westos.path[root@localhost mnt]# patch -b westos westos.pathpatching file westos[root@localhost mnt]# lswestos  westos1  westosdir  westosdir1  westos.orig  westos.path[root@localhost mnt]# cat westos.orig  #备份的原文件hello westos[root@localhost mnt]# cat westoshello westos123[root@localhost mnt]# cat westos1hello westos123

3.cut

cut -d :	##指定:为分隔符-f 	##指定显示的列 5第五列| 3,5 3和5列|3-5 3到5列|5- 第五列以后|-5 到第五列-c	##指定截取的字符(数字用法同-f)
[root@localhost mnt]# grep bash -v /etc/passwd | cut -d : -f 1bindaemonadmlpsyncshutdownhaltmail

4.sort

sort-n	##纯数字排序-r	##倒叙-u	##去掉重复-o	##输出到指定文件-t	##指定分隔符-k	##指定排序的列
[root@localhost mnt]# sort -n westos1256889912266868[root@localhost mnt]# sort -nr westos6868261299886521[root@localhost mnt]# sort -nr westos -o westos1[root@localhost mnt]# cat westos16868261299886521[root@localhost mnt]# sort -u westos112226566889[root@localhost mnt]# sort -t : -k 2 westos6:12:122:28:265:52:60:688:680:80:80:91:9[root@localhost mnt]# sort -t : -k 1 westos0:680:80:80:91:92:122:22:65:56:18:268:68

5.uniq

uniq-c	#合并重复并统计重复个数-d	#显示重复的行-u	#显示唯一的行
  • 1.ifconfig 网卡 可以显示此网卡的信息
    显示信息中包含此网卡使用的ip地址
    请用命令过滤此ip并在输出时只显示ip其他信息不显示
[root@localhost mnt]# ifconfig enp1s0 | head -n  2 | tail -n 1 | cut -d " " -f 101.1.1.88
  • 2.找出能登陆系统用户中UID最大的用户,并显示其名称
[root@d mnt]# grep bash /etc/passwd | sort -t : -k 3 -nr | head -n 1 | cut -d : -f 1lee
  • 3.当前主机为web服务器,请抓取访问web服务器次数排在前5的ip地址
[root@localhost mnt]# cut -d " " -f 1 /etc/httpd/logs/access_log* | sort -n | uniq -c | sort -nr | head -n 5 | cut -d " " -f 7[root@localhost 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 2172.25.254.12172.25.254.212172.25.254.13

6.tr

tr 'a-z' 'A-Z'		##小写转大写tr  'A-Z' 'a-z'		##大写转小写[root@localhost mnt]# ifconfig enp1s0 | head -2 | tail -1 | cut -d " " -f 101.1.1.88[root@localhost mnt]# ifconfig enp1s0 | head -2 | tail -1 | cut -d " " -f 10 | tr '1' '*'*.*.*.88[root@localhost mnt]# lspasswd  westos[root@localhost mnt]# ls | tr  'a-z' 'A-Z'PASSWDWESTOS

7.test

test = []	##[] 就相当于test命令"test $a = $b" = [ "$a" = "$b" ]test数字对比=!=-eq		##等于-ne		##不等于-le		##小于等于-lt		##小于-ge		##大于等于-gt		##大于test的条件关系-a	##并且-o	##或者test对空的判定-n	##nozero 判定内容不为空-z	##zero	判定内容为空test对于文件的判定-ef		##文件节点号是否一致(硬链)-nt		##文件1是不是比文件2新-ot		##文件1是不是比文件2老-d		##目录-S		##套结字-L		##软连接-e		##存在-f		##普通文件-b		##快设备-c		##字符设备
[root@node1 ~]# a=1[root@node1 ~]# b=1[root@node1 ~]# test "$a" = "$b" && echo yes || echo noyes[root@node1 ~]# [ "$a" -eq "$b" ] && echo yes || echo no  ##等于[root@node1 ~]# [ "$a" -ne "$b" ] && echo yes || echo no  ##不等于[root@node1 ~]# [ "$a" -le "$b" ] && echo yes || echo no ##小于等于[root@node1 ~]# [ "$a" -ge "$b" ] && echo yes || echo no  ##大于等于[root@node1 ~]# [ "$a" -lt "$b" ] && echo yes || echo no  ##小于[root@node1 ~]# [ "$a" -gt "$b" ] && echo yes || echo no  ##大于[root@node1 ~]# [ "$a" -gt "0" -a "$a" -lt "10" ] && echo yes || echo no  ##并且[root@node1 ~]# [ "$a" -le "0" -o "$a" -ge "1" ] && echo yes || echo no  ##或者[root@node1 ~]# [ "$a" -gt "0" ] && [ "$a" -lt "10" ] && echo yes || echo no[root@node1 ~]# [ "$a" -gt "0" ] ||  [ "$a" -lt "1" ] && echo yes || echo no[root@node1 ~]# [ -z "$c" ] && echo yes || echo no   ##zero	判定内容为空yes[root@node1 ~]# [ -n "$c" ] && echo yes || echo no   ##nozero 判定内容不为空no
[root@node1 mnt]# [ "/mnt/westos" -ef "/mnt/westos1" ] && echo yes || echo nono[root@node1 mnt]# [ "/mnt/westos" -nt "/mnt/westos1" ] && echo yes || echo noyes[root@node1 mnt]# [ "/mnt/westos" -ot "/mnt/westos1" ] && echo yes || echo nono[root@node1 mnt]# [ -d "/mnt/"  ] && echo yes || echo noyes[root@node1 mnt]# ln -s /mnt/westos file1[root@node1 mnt]# ln /mnt/westos /mnt/file[root@node1 mnt]# [ -L "/mnt/file1"  ] && echo yes || echo noyes[root@node1 mnt]# [ -S "/mnt/file"  ] && echo yes || echo nono[root@node1 mnt]# [ -e "/mnt/file"  ] && echo yes || echo noyes[root@node1 mnt]# [ -c "/dev/vda"  ] && echo yes || echo nono[root@node1 mnt]# [ -b "/dev/vda"  ] && echo yes || echo noyes
  • 执行下列脚本来判断用户类型
    user_check.sh 用户
    用户类型为
    super user #uid=0
    system user #uid<1000,并且不能用于登陆操作系统
    common user #uid>=1000
[root@d mnt]# vim usercheck.sh  1 #!/bin/bash  2 [ -z "$1" ] && {
3 echo "error:please input username following script!" 4 exit 5 } 6 id $1 &> /dev/null || {
7 echo "user $1 is not exist!" #当不能扔进垃圾箱时表示没有该用户 8 exit 9 } 10 11 USER_UID=$( id -u $1 ) 12 USER_SHELL=$( grep $1 /etc/passwd | cut -d : -f 7 ) 13 [ "$USER_UID" -eq "0" ] && {
14 echo $1 is supper user 15 exit 16 } 17 [ "$USER_UID" -lt "1000" ] && [ "$USER_SHELL" != "/bin/bash" ] && {
18 echo $1 is system user 19 exit 20 } 21 [ "$USER_UID" -ge "1000" ] && [ "$USER_SHELL" = "/bin/bash" ] && {
22 echo $1 is common user 23 exit 24 } 25 echo "unknow user type" 测试:[root@d mnt]# sh usercheck.sh westoswestos is common user[root@d mnt]# sh usercheck.sh linuxuser linux is not exist![root@d mnt]# sh usercheck.sh leelee is common user[root@d mnt]# sh usercheck.sh nginxnginx is system user[root@d mnt]# sh usercheck.sh rootroot is supper user
  • 编写脚本完成以下条件
    file_check.sh 在执行时
    如果脚本后未指定检测文件报错“未指定检测文件,请指定”
    如果脚本后指定文件不存在报错“此文件不存在”
    当文件存在时请检测文件类型并显示到输出中
[root@d mnt]# vim filecheck.sh   1 #!/bin/bash  2 [ -z "$1" ] && {
3 echo error: please input file following script! 4 exit 5 } 6 ls -ld $1 > /dev/null || [ -e "$1" ] || {
7 echo error:not found $1 8 exit 9 } 10 [ -d "$1" ] && {
11 echo $1 is 目录 12 exit 13 } 14 [ -S "$1" ] && {
15 echo $1 is 套接字 16 exit 17 } 18 [ -L "$1" ] && {
19 echo $1 is 软连接 20 exit 21 } 22 [ -f "$1" ] && {
23 echo $1 is 普通文件 24 exit 25 } 26 [ -b "$1" ] && {
27 echo $1 is 快设备 28 exit 29 } 30 [ -c "$1" ] && {
31 echo $1 is 字符设备 32 exit 33 }测试:[root@node1 mnt]# sh file_check.sh /mnt/westos/mnt/westos is 普通文件 [root@westoslinux mnt]# sh file_check.sh /mnt//mnt/ is 目录

8.&& ||

&&	符合条件作动作||	不符合条件作动作

补充:$的常见用法

链接: .

转载地址:http://dnhzi.baihongyu.com/

你可能感兴趣的文章
AJAX应用之注册用户即时检测
查看>>
File 类小结
查看>>
java除去字符串空格
查看>>
jsp 2.0标记文件
查看>>
Hibernate中Criteria的完整用法
查看>>
sql jsp
查看>>
spring beans beanfactory applicationcontext
查看>>
使用ORM工具进行数据访问
查看>>
使用ORM工具进行数据访问
查看>>
编译与部署Eclipse+Tomcat+MySQL+Liferay4.1.2
查看>>
POJ3728,The merchant(倍增LCA+分治)
查看>>
2019 ICPC Malaysia National,E. Optimal Slots(01背包变形)
查看>>
洛谷P1638 逛画展(双向队列)
查看>>
POJ2892,Tunnel Warfare(线段树维护连续区间)
查看>>
POJ3468,A Simple Problem with Integers(线段树-区间查询-区间更新)
查看>>
杭电ACM——6463(思维)
查看>>
杭电ACM——2069,Coin Change(DP)
查看>>
杭电ACM——2110,Crisis of HDU(母函数)
查看>>
杭电AM——2152,Fruit(母函数)
查看>>
杭电ACM——2566,统计硬币(DP)
查看>>