Home   Profile   Fun
#102 Linux  03.04.2007

How to use fdisk within a shell script


This can be done easily with a here-document.
#!/bin/bash

VSIZE=200

fdisk /dev/hda >/dev/null 2>&1 <<EOF
n
p
3

+${VSIZE}M
w
EOF

exit

This script creates a third primary partition with a size of 200Mb. It passes all the parameters to fdisk you would normally enter by hand. Output and Errors are suppressed by redirecting stdout and stderr to /dev/null.