shell 自动交互式处理命令的两种方法实践

1:use echo -e

example:

echo -e "n\np\n1\n\n+100M\nn\np\n2\n\n+1024M\nw\n"|fdisk /dev/vdb

\n代表回车

[root@server7 ~]# fdisk -l /dev/vdb

Disk /dev/vdb: 8589 MB, 8589934592 bytes
16 heads, 63 sectors/track, 16644 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

echo -e "n\np\n1\n\n+100M\nn\np\n2\n\n+1024M\nw\n"|fdisk /dev/vdb &> /dev/null

[root@server7 ~]# fdisk -l /dev/vdb

Disk /dev/vdb: 8589 MB, 8589934592 bytes
16 heads, 63 sectors/track, 16644 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x99657b78

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1               1         204      102784+  83  Linux
/dev/vdb2             205        2286     1049328   83  Linux
[root@server7 ~]#

ok   




2.expect:   

  notice:  you should  use “yum install expect” at begining。


  1 #!/usr/bin/expect                                                                                                                                                        
  2 spawn fdisk  /dev/vdb
  3 expect Command
  4 send "n\r"
  5 send "p\r"
  6 send "1\r"
  7 send "\r"
  8 send "+100M\r"
  9 expect "Command"
 10 
 11 
 12 send "n\r"
 13 send "p\r"
 14 send "2\r"
 15 send "\r"
 16 send "+1G\r"
 17 send "w\r"
 18 expect eof

两者异曲同工!~~~~~~~~~~~~

选择自己喜欢的把 对了,在bash里面调用expect的话格式如下

/usr/bin/expect <<-EOF
具体内容~~~~~
EOF

是不是很方便呢 ~~                        

这样我们再来一个例子:使用lftp下载一个文档:

[root@server7 ~]# lftp 172.25.254.250
lftp 172.25.254.250:~> cd pub/linux/pub/docs/lftp 
172.25.254.250:/pub/linux/pub/docs> ls vi.png
lftp 172.25.254.250:/pub/linux/pub/docs> ls vi.png-rwxrwxrwx    1 0        0          310741 Dec 25  2013 vi.png
lftp 172.25.254.250:/pub/linux/pub/docs> get vi.png

ok,这是我们常规的操作,此时我们首先使用方法一也就 echo -e

example:

[root@server7 ~]# rm vi.png 
rm: remove regular file `vi.png'? yes

先删掉

[root@server7 ~]# echo -e "cd pub/linux/pub/docs/\nls vi.png\nget vi.png\n"|lftp 172.25.254.250 
-rwxrwxrwx    1 0        0          310741 Dec 25  2013 vi.png
[root@server7 ~]# ls vi.png 
vi.png

ok 就这么easy

下来我们bash里调用expect 再来解决

 

1 #########################################################################   
  2 # File Name: get.sh
  3 # Author: dty
  4 # mail: 576786031@qq.com
  5 # Created Time: Fri 14 Aug 2015 03:15:55 PM CST
  6 #########################################################################
  7 #!/bin/bash
  8 /usr/bin/expect <<-EOF
  9 spawn lftp 172.25.254.250
 10 expect lftp
 11 send "cd pub/linux/pub/docs\r"
 12 sleep 1
 13 send "ls vi.png\r"
 14 send "get vi.png\r"
 15 send "quit\r"
 16 expect eof
 17 EOF

~             

[root@server7 ~]# ./get.sh 
spawn lftp 172.25.254.250
lftp 172.25.254.250:~> cd pub/linux/pub/docs
cd ok, cwd=/pub/linux/pub/docs
lftp 172.25.254.250:/pub/linux/pub/docs> ls vi.png
-rwxrwxrwx    1 0        0          310741 Dec 25  2013 vi.png
lftp 172.25.254.250:/pub/linux/pub/docs> get vi.png
310741 bytes transferred
lftp 172.25.254.250:/pub/linux/pub/docs> quit
[root@server7 ~]#

okok顺利完成




                                                                                                                                   很简单吧 get起来  感谢开源                        -单纯酱






转载于:https://my.oschina.net/loveleaf/blog/492271

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值