#!/bin/bash
Hey folks
So I am sitting with a conundrum... am attempting to call a function from with a loop that resides with another loop. Problem being that the two loops do not wait for input... they simply continue to execute and with that my required user input is ignored and so the function fails. So am essentially looking for a way to call the function, prompt the user... and use the answer to steer the function into doing one of 4 options.
Have googled "call a function from with a loop, wait for it to complete then continue bash", "wait for function to complete within loop before proceeding bash", "stop loop until condition met in bash" amongst others
So the basic layout would look something like this
# this resides within a function that forms a part of a larger script
'bash'
while IFS='\n' read -r line; do
while IFS1=' ' read -r part_name part_size part_uuid mount_point; do
if [[ ${part_uuid} = "" ]]; then
FORMAT_FUNCTION ${part_name}
fi
done
done
# this is separate function called from above
'bash'
function FORMAT_FUNCTION {
local_partition=${1}
if [[ ! ${local_partition} = "" ]]; then
echo "Next we need to select the Filesystem type"
echo ""
echo "1 - ext2"
echo "2 - ext3"
echo "3 - ext4"
echo "4 - fat"
read -n1 -r -p "Please enter a option from above" key
if [[ ${key} = "1" ]]; then
sudo mkfs -t ext2 /dev/${local_partition}
elif [[ ${key} = "2" ]]; then
sudo mkfs -t ext3 /dev/${local_partition}
elif [[ ${key} = "3" ]]; then
sudo mkfs -t ext4 /dev/${local_partition}
elif [[ ${key} = "4" ]]; then
sudo mkfs -t vfat /dev/${local_partition}
fi
fi
}
What I have tried:
So I have attempted using PID's (with additional loops to wait for the completion of the function) and using the built in wait functions with no success. Looked into using sub processes however I fear that I may not have fully understood the syntax required to run them correctly