shell --常用命令

diff

diff:用来比较两个文件或者目录的不同

常用参数:
-b    不检查空格字符
-B    不检查空行
-c    显示全部内容并且标出不同
-i    检查大小写不同
-q    仅仅显示有无差异,不显示详细信息
-r    比较子目录中的文件
-u    以合并的方式来显示文件内容的不同
diff在比较文件过程中结果读取方式:

a   添加
c   更改
d   删除

例:

vim file1
{
白茶清欢无别事
我的名字
}
vim file2
{
白茶清欢无别事
}
diff file1 file2

diff file2 file1

vim file2
{
白茶清欢无别事
我得名字
}
diff file1 file2

vim file2
{
白茶清欢无别事(空格字符)
我得名字
}

diff -b file1 file2   #不检测空格字符

vim file2
{
白茶清欢无别事

我得名字
}

diff -B file1 file2   #不检测空行

diff -c file1 file2   #显示所有内容并,标出不同之处

mkdir westos1 westos2
mkdir /westos1/westos3
diff -r westos1 westos2

diff -u file1 file2

diff -u file1 file2 > file.path

pacth

patch : 用于文件不同文件打补丁
-b

cat file.path
patch -b file1 file.path  #加上-b不会删除源文件,并且会生成file1.orig文件
cat file1 file2

cut

cut   #用于字符截取
常用参数:
-d   指定分割符
-f   截取的列
-c   截取字符的位置

例:

截取 172.25.254.251

ifconfig eth0
{
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.251  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::5054:ff:fe00:30a  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:00:03:0a  txqueuelen 1000  (Ethernet)
        RX packets 168357  bytes 15229316 (14.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6098  bytes 883367 (862.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
}
   
ifconfig eth0 | cut -d " "  -f  10 | head -n 2 | tail -n 1
ifconfig eth0 | awk '/inet\>/{print $2}'        #截取ifconfig eth0信息中 172.25.254.251
   

sort

sort  #用于字符排序
常用参数:
-n  纯数字排序
-r  倒序
-u  去掉重复数字
-o  输出到指定文件中
-t  指定分割符
-k   指定排序的列
cp /etc/*.conf  /mnt

ls -l | sort -t " " -k 5 -nr | head -n 5 | cut -c 43-   

ls -S  | head -n 5    #按第5列排序并且输出最大的文件名称

uniq

uniq   #对重复的字符作相应处理
常用参数:
-u  显示唯一的行
-d  显示重复的行
-c  每行显示一次并统计重复次数

&& 和 ||

&& 用来执行条件成立执行的命令

|| 用来执行条件成立执行的命令


vim ping.sh
{

#!/bin/bash
ping -c1 -w1  172.25.254.111 &> /dev/null  &&{
     echo 172.25.254.111 is up
}||{
     echo 172.25.254.111 is down
}

}

sh ping.sh 172.25.254.111   #拔掉网线测试
172.25.254.111 is down

sh ping.sh 172.25.254.111
172.25.254.111 is up

tr

tr   #大小写转化

test

test和[]等同   使用[]注意内容前后必须加上空格  [空格 内容 空格]
常用参数:
-z   #为空
-n   #不为空
-ef  #节点号是否相同
-nt  #file1是否比file2建立的晚
-ot  #file1是否比file2建立的早
-e   #文件存在
-f   #文件存在并且是正规文件
-L   ##软链接	ln -s file westos	ll westos
-s   ##socket   套接字	 yum install -y mariadb-server  ll /var/lib/mysql/
-b   ##block	块设备    ll /dev/sda
-d   ##目录
-c   ##字符设备	ls /dev/pts	 date > /dev/pts/5
=    #等于    
-lt  #小于
-le  #小于等于
-ge  #大于等于
-gt  #大于
 a=1;b=2
 [ "$a" = "$b" ] && echo yes || echo no     
 [ "$a" -lt "$b" ] && echo yes || echo no     
 [ "$a" -le "$b" ] && echo yes || echo no   
 [ "$a" -ge "$b" ] && echo yes || echo no
 [ "$a" -gt "$b" ] && echo yes || echo no
 a=2;b=2
 [ "$a" -ge "$b" ] && echo yes || echo no
vim test.sh
{
#!/bin/bash
[ "$1" -gt "0" -a "$1" -le "10" ]&& echo yes || echo no   #判断一个数是否属于0-10

}
sh test.sh 5
yes
sh test.sh 11
no

#判断/的内存使用率,当超过30%时在/var/log/messages下发出报警

vim mnt.sh
{
#!/bin/bash
[ `df / | tail -n 1 | awk '/\/$/{print $5}' | cut -d % -f 1` -ge "30" ]&&{
    echo " Waring: System root is full !!" >> /var/log/messages
}
}             

sh  mnt.sh

#判断文件属性
{
#!/bin/base
[ -z "$1" ] &&{     #判断文件是否为空
     echo please input filename following scripts
     exit
}
[ -e "$1" ] ||{    #判断文件是否存在
     echo $1 is not exit
     exit
}
[ -L "$1" ] &&{    #判断文件是否是软连接
     echo $1 is softlink file
     exit
}
[ -f "$1" ] &&{    #判断文件是否是正规文件
     echo $1 is common file
     exit
}
[ -d "$1" ] &&{    #判断是否是个目录
     echo  $1 is directory
     exit
} 
[ -b "$1" ] &&{    #判断是否是个块设备
     echo $1 is block
     exit
}
[ -c "$1" ] &&{   #判断是否是字符设备
     echo  is acharacter device
     exit
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值