sort、tr、uniq工具和免交互expect

sort

格式:
sort [选项] [参数]
-f #忽略大小写
-b #忽略每行前面的空格
-M #按照月份进行排序
-n #按照数字排序
-r #反向排序
-u #等同于uniq,表示相同的数据仅显示一行
-t #指定分隔符,默认使用tab键分隔
-o #<输出文件>:将排序后的结果转存至指定文件
-k #指定排序区域 

tr

格式:
tr [选项] [参数]

参数:
-c:取代所有不属于第一字符集的字符
-d:删除所有属于第一字符集的字符
-s:把连续重复的字符以单独一个字符表示
-t:先删除第一字符集较第二字符集多出的字符

cut

cut :列截取工具
选项 :
-b :按字节截取
-c :按字符截取,常用于中文
-d :指定以什么为分隔符截取,默认为制表符
-f :通常和-d一起用,匹配第几列

uniq

格式:
uniq [选项] 参数

参数:
-c	进行计数
-d	仅显示重复行
-u	仅显示出现一次的行

免交互

EOF

[root@localhost ~]# cat <<EOF            #开头以<<加EOF,这里的EOF可以换成其他的字符串
> hello
> world
> EOF                                     #以EOF结尾
hello
world
[root@localhost ~]# cat > /etc/yum.repos.d/yum.repo <<EOF       #免交互编写yum本地仓文件
> [test]
> name=test
> baseurl=file:///mnt
> enabled=1
> gpgcheck=0
> EOF
[root@localhost ~]# cat /etc/yum.repos.d/yum.repo
[test]
name=test
baseurl=file:///mnt
enabled=1
gpgcheck=0
[root@localhost ~]# vim sy.sh
#!/bin/bash
cat <<-EOF                 #EOF前加-可以去除字符串中的制表符(tab键)
        hello
EOF

cat <<EOF
        hello
EOF
[root@localhost ~]# bash sy.sh
hello
	hello	

[root@localhost ~]# vim w.sh
#!/bin/bash
grep $1 <<'EOF'                  #加上单引号或者加\EOF表示对交互内容中的所有特殊字符转义
pete    $100
joe     $200
sam     $ 25
bill    $ 9
EOF
[root@localhost ~]# ./w.sh bill
bill	$ 9
[root@localhost ~]# vim w.sh
#!/bin/bash
grep $1 <<-'EOF'                     //-表示忽略前导制表符
        pete    $100
joe     $200
        sam     $ 25
bill    $ 9
EOF
[root@localhost~ ]# ./w.sh bill      
bill	$ 9

expect

[root@localhost ww]# yum install -y expect        #下载expect
[root@localhost~]# vim ss.sh 
#!/usr/bin/expect              
#设置超时时间10秒:
set timeout 10  
set user root               #设置变量
set ip 192.168.43.100        
set pass 123456
spawn ssh $user@$ip了,            #启动进程,并跟踪后续交互信息
expect {
        "(yes/no)"             #期望的字符
        {send "yes\r";}       #交互的字符
        "password:"
        {send "$pass\r";}
}
#interact                   

expect "#"
send "ls\r"
send "ifconfig ens33\r"
send "exit\r"                      #让它不再期待
expect eof                       
[root@localhost ~]# ./ss.sh 
spawn ssh root@192.168.43.100
root@192.168.43.100's password: 
Last login: Thu Jun 10 11:47:17 2021 from 192.168.43.55
[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  J  user.sh  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.100  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::976c:f4a0:edcc:9ae7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:3f:67:c5  txqueuelen 1000  (Ethernet)
        RX packets 657550  bytes 929968663 (886.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 269078  bytes 18774253 (17.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# exit
登出
Connection to 192.168.43.100 closed.

#!bin/bash             
expect -c "               //在bash环境引用expect
spawn ssh root@192.168.43.100
expect \"password:\" { send \"123456\r\" }
expect \"]#\" { send \"infconfig ens33\r\" }
expect eof
"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值