首页 > 系统服务 > 详细

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

时间:2015-08-14 13:57:05      阅读:290      评论:0      收藏:0      [点我收藏+]

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

是不是很方便呢 ~~                                                       -单纯酱

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

原文:http://my.oschina.net/loveleaf/blog/492271

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!