Shell Scripting Error - help

I am trying to do an upgrade on a VMWare appliance. I am getting an error, and I dug into the script that is causing the error and will post the statement that is causing the error. Basically, my software seems to want an additional partition before it will do the upgrade. So, I added a new partition to the VM and left it in every possible state - formatted, not formatted, mounted, not mounted.. etc. I am going to post the script, if you can help me understand what the fdisk -l (list fdisk) output is causing this to error - I would appreciate it. I will also post the section that is next which is what would happen if it succeeded. Thank you in advance for your help.

function vmware_check_additional_disk()
{
if [ "$MODEL" == "VMWARE" ]; then
LogMessage "INFO" "VMWARE - verify additional disk is available..."
TMPFILE1=`mktemp -t ap.tmp.XXXXXXXX` || exit 1
TMPFILE2=`mktemp -t ap.tmp.XXXXXXXX` || exit 1
# Require additional disk added to the VM first if not abort.
RESET_HDD=$(fdisk -l 2>&1 | grep "contain a valid partition table" | awk '{print $2}' | tail -n1)
if [ -z $RESET_HDD ]; then
rm -f /tmp/ap.tmp.*
LogMessage "ERROR" "VMWARE - Unable to locate new empty partition added to the VM..."
return 1
fi
RESET_PARTITION="${RESET_HDD}1"
echo "p\n" > $TMPFILE1
echo "q\n" >> $TMPFILE1
fdisk $RESET_HDD < $TMPFILE1 > $TMPFILE2 2>&1
HDD_SIZE_TMP=$(egrep '^Disk' $TMPFILE2 | awk '{print $5}')
is_integer $HDD_SIZE_TMP
if [ $? -ne 0 ]; then
rm -f /tmp/ap.tmp.*
LogMessage "ERROR" "VMWARE - Unable to determine disk size $HDD_SIZE bytes..."
return 1
fi
# convert bytes to MG
HDD_SIZE=$(($HDD_SIZE_TMP>>20))
if [ $HDD_SIZE -lt 2000 ]; then
rm -f /tmp/ap.tmp.*
LogMessage "ERROR" "VMWARE - Can't setup reset image in partition smaller than 2000 MB"
return 1
fi
rm -f /tmp/ap.tmp.*
fi

Odd construct that. How about a here doc?

fdisk $RESET_HDD <<EOF  > $TMPFILE2 2>&1
p
q
EOF

I don't think there should be an extra newline there. which the shell script could insert. I may be wrong. Can you run fdisk with the value of $RESET_HDD manually, emulating what the code is trying to do: enter a p and enter a q

If you can decide what $TMPFILE2 and $TMPFILE1 translate to, try

cat $TMPFILE1
cat $TMPFILE2

This will tell you if you got a straight p and q. Some versions of echo do NOT by default decode \n into a newline. You may be getting garbage like "pn". fdisk will produce an error as well that you can see.

What the script code is doing is running the fdisk program "interactively" with canned responses. A more reasonable approach is a here doc, as above.

Do you have nonstandard aliases or symlinks for the shells - /bin/bash especially? what does the output of

type echo

give? It should be "echo is a shell builtin". The "real" bash echo will ignore those \n characters. Otherwise you may get garbage in $TMPFILE1

type echo
echo is a shell builtin
echo "p\n"> file
echo "q\n" >> file
cat file

gives

p
q

NO extra newline characters or garbage extraneous characters.