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 represents a carriage return
[Email protected] ~]# fdisk-l/DEV/VDB
disk/dev/vdb:8589 MB, 8589934592 bytes
Heads, Sectors/track, 16644 cylinders
Units = Cylinders of 1008 * 516096 bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): 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
Heads, sectors/t Rack, 16644 cylinders
Units = cylinders of 1008 * = 516096 bytes
Sector size (logical/physical): bytes/512 Bytes
I/o size (minimum/optimal): 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
The two are similar! ~~~~~~~~~~~~
Choose the right one you like and call expect in Bash in the following format
/usr/bin/expect <<-eof
Specific content ~~~~~
Eof
is not very convenient ~ ~-Simple sauce
Two methods of shell automatic Interactive processing of commands