problem in automating "fdisk" command using send and expect

hi
i want to automate fdisk command .
i spawned a process containing fdisk command from a process
and tried to send the options to fdisk promt from that process.
but that spawed process is notstarting itself

help me out

trying for two days
:wall:

my code:

#!/bin/bash
echo starting in send
cat <<EOF > /root/test_ntp/send1
#!/usr/bin/expect -f
spawn sh spawn_process
send -- "m\n"
send -- "l\n"
send -- "q\n"
EOF
chmod 755 send1
./send1
rc=$?
if [ $rc -ne 0 ]
then
  echo failed
else
  echo passed
fi
echo done in send

I don't know what you're trying to accomplish, but I don't see fdisk anywhere in your code.

fdisk is in separate file

thats the spawned process spawn_process

code for that spawn_process

#!/bin/bash
fdisk /dev/xvdj

:slight_smile:

OK, I did some test and here's the conclusion:

If you spwan a process in Expect and don't literally "expect" the process's output but "send" command immediately, Expect will send command *before* the process even ready to accept inputs. Thus a better way is to actually "expect" something before "send":

expect "m for help" {
    send "m\n"
}
expect "m for help" {
    send "l\n"
}
expect "m for help" {
    send "q\n"
}

Good luck.

1 Like

Expect script is useless here, just perform your tasks through the pure shell script
An example

echo "n
p
1


w" | fdisk /dev/sdb

just modify the parameters to suit your requirements

1 Like

thanks to both of you.It worked. :b:

---------- Post updated at 01:24 AM ---------- Previous update was at 01:21 AM ----------

thanks to every one

another way of doing this

fdisk "device to be partitioned" <<EOF
n
p
1

w
EOF