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代表回车
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]#
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
是不是很方便呢 ~~ -单纯酱