justkar4u has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I am trying to automate partitioning and formatting on Linux. So using fdisk command. Now the problem is when am passing piped inputs to the fdisk it is working correctly as expected. I also wanted to pass arguments from command line to my perl script. But it just stops after entering the interactive command prompt of fdisk when I try to pass arguments from command line! Is there anyway I can get this to work ? Does fdisk allow all the command line arguments at once(since it has kind of sub menus)? or is there anyway that I can automate such process by passing args through command line ??
# # Include Files # use strict; use warnings; # # Variable Declarations and Initialization # my $char = '"'; my $cmd; our $device = ''; # # Main Script Body # #check if device name is supplied if($#ARGV == 0) { $device = $ARGV[0]; # store device name in variable # concatenate device name with fdisk command <b> $cmd = 'fdisk -c'.' '.$device.' < fdisk.input';</b> ###### +#This works system $cmd; exit 0; } else { print "Format Error:Usage- partition.pl <device_name>\n"; print "Example: partition.pl "; print $char; print "/dev/sdf"; print "$char\n"; exit -1; # displayed as 255 } ###### fdisk.input has these params: n p 1 1 +G t 83 w ####
## this modification does not work :(
$cmd = 'fdisk -c'.' '.$device.' n p 1 1 +G t 83 w'; # this displays fdisk usage error
nor does this work
$cmd = 'fdisk -c'.' '.$device; system $cmd; system " n p 1 1 +G t 83 w"; ## this however enters the fdisk prompt and waits for the above inputs + !!
Thanks. - Ram

Replies are listed 'Best First'.
Re: fdisk automation
by Corion (Patriarch) on May 27, 2011 at 19:00 UTC

    system does not send keys to other programs, so your approach cannot work.

    I recommend using sfdisk instead of fdisk, because sfdisk takes all parameters on the command line.

    If you really, really want to automate entering keys into fdisk, take a look at Expect, or treat fdisk as a pipe:

    my $cmd = "/usr/sbin/fdisk -c $device"; open my $fdisk, "| $cmd" or die "Couldn't spawn [$cmd]: $!"; for my $command ("n", "p", "1", "1", "+G", "t", "83", "w") { print $fdisk "$command\n"; };
      Thanks a lot !