Two methods of shell automatic Interactive processing of commands

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.