Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#!/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
Posted
Updated 21-Dec-22 0:32am
Comments
Richard MacCutchan 21-Dec-22 4:17am    
I just tried the code in your first example, and it waits for input at both places.
J de Villiers 21-Dec-22 6:12am    
Thanks Richard am aware of that... however in the context of it does not! However did find a solution read the solution below

1 solution

So I changed the approach... instead of the conventional looping systems I created an artificial one.
Started by outputting "lsblk -o NAME,SIZE,UUID,MOUNTPOINT | grep ${activeDevice}" before and after to a separate files, using the "diff" command and out put that to a file, following which I used the "$(head -n 1 lsblk_diff | awk '{print $1}')" on the diff file to determine the partitions' names that I edited and then created a new function. In the new function using if statements and the first two files along with the grep command, read the ${partName} variables to local variables and compared them. Next I used the "sed -i '/'"${partName}"'/d' lsblk_diff" to remove the lines from the "lsblk_diff" file. Used "if [[ ! $(cat lsblk_diff) = "" ]]; then" to recall the function and as there was no loop forcing the progress anymore the rest is working
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900