Shell脚本----(sort、tr、uniq工具和免交互expect)

sort工具

常用选项:

  -f :忽略大小写
  -b :忽略每行前面的空格
  -M :按照月份进行排序
  -n :按照数字排序
  -r :反向排序
  -u :等同于uniq,表示相同的数据仅显示一行
  -t :指定分隔符,默认使用tab键分隔
  -o <输出文件>:将排序后的结果转存至指定文件
  -k :指定排序区域 
[root@localhost ww]# cat /etc/passwd | sort -n -t: -k3   //以冒号为分隔符,将第三列按数字排序,加-r则倒序显示
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@localhost ww]# cat test.txt 
apple
peache 
pear

pear'
banana
apple
apple
orange
orange
uniq 选项 参数
uniq -c :进行计数
-d :仅显示重复行
-u :仅显示出现一次的行
[root@localhost ww]# nl test.txt | sort | uniq -c    
      2        
      1      1	apple
      1      2	peache 
      1      3	pear
      1      4	pear'
      1      5	banana
      1      6	apple
      1      7	apple
      1      8	orange
      1      9	orange
[root@localhost ww]# cat test.txt | sort | uniq -d    //只显示重复的

apple
orange

[root@localhost ww]# cat test.txt | sort -u   //去除重复行
apple
banana
orange
peache 
pear
pear'

[root@localhost ww]# cat test.txt | uniq      //uniq工具将连续重复的行去除
apple
peache 
pear

pear'
banana
apple
orange
[root@localhost ww]# who | awk '{print $1}' | uniq -d
root
[root@localhost ww]# last | awk '{print $1}' | sort | uniq -d
root
[root@localhost ww]# cat test.txt | sort | uniq -u       //只显示不重复的行
banana
peache 
pear
pear'

tr工具

tr 选项 参数
-c :取代所有不属于第一字符集的字符
-d :删除所有属于第一字符集的字符
-s :把连续重复的字符以单独一个字符表示
-t :先删除第一字符集较第二字符集多出的字符
[root@localhost ww]# cat test.txt | tr 'apple' 'star'   //一一对应替换字符
saarr
arschr 
arsr

arsr'
bsnsns
saarr
saarr
orsngr
orsngr
[root@localhost ww]# nl test.txt | tr -d 'a'          //删除字符
     1	pple
     2	peche 
     3	per
       
     4	per'
     5	bnn
     6	pple
     7	pple
     8	ornge
     9	ornge
[root@localhost ww]# cat test.txt | tr -s 'p'          //将重复的字符以单个字符显示
aple
peache 
pear

pear'
banana
aple
aple
orange
orange
[root@localhost ww]# cat test.txt | tr -d '\n       //删除回车查看文件
[root@localhost ww]# cat test.txt | tr -s '\n       //删除空行查看文件
[root@localhost ww]# cat test.txt | tr "'" '/'      //将单引号替换成/

cut :列截取工具
选项 
-b :按字节截取
-c :按字符截取,常用于中文
-d :指定以什么为分隔符截取,默认为制表符
-f :通常和-d一起用,匹配第几列
[root@localhost ww]# cat /etc/passwd | cut -d: -f3    //以冒号为分隔符查看文件的第三列
0
1
2
3
4
5
6

shell脚本EOF免交互

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

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

expect免交互

  • expect
    判断上传输出结果中是否包含指定的字符串,如果有则立即返回,否则就等待超时时间后返回,只能捕捉由spawn启动的进程的输出,用于接受命令执行后的输出,然后期望的字符串匹配
  • send
    向进程发送字符串,用于模拟用户的输入,该命令不能自动回车换行,一般要加\r(回车)
[root@localhost ww]# yum install -y expect        //先下载expect工具

定义参数:

set timeout 20      //登录超时多少秒退出
log_file test.log   //日志记录操作
log_user 1          //1为屏幕输出信息,0为不输出

编写脚本用expect实现ssh远程登录并执行操作

[root@localhost ww]# vim ss.sh 
#!/usr/bin/expect              
#设置超时时间10秒:
set timeout 10  
set user root                //set设置变量
set ip 192.168.43.100        
set pass 123456
spawn ssh $user@$ip了,            //spawn,启动进程,并跟踪后续交互信息
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 ww]# ./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
"
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值