LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash looping or function within a function (https://www.linuxquestions.org/questions/programming-9/bash-looping-or-function-within-a-function-438390/)

FirmbIT 04-24-2006 12:08 PM

Bash looping or function within a function
 
Hello, I have a script that will go to a backup server and check for a backup server for a specific domain. This is all within a specific function, the thing is, if for some reason the backup does not exist on that backup server, I want it to ask the user if they want to try a different backup server, read the input and go back to the section within the function where it asks for the server and goes from there. I tried a while loop but that breaks the script, and I also do not want to make another function with the same code in it as it gets too confusing then. How can I do this without making it too complex? Here is the section of the code. Remember, this code is inside of a function:

Code:

echo ""
                echo "[Backup Servers]"
                echo "----------------------------------------------------------------"
                grep -ilR $host /root/server_backupserver/cron/ | cut -d. -f2
        while [ ! $server ]
        do
                        echo "----------------------------------------------------------------"
                        echo -n "Pick a Backup Server: "
                        read server
                        echo "----------------------------------------------------------------"
        done
                homedir=$(ssh $server crontab -l | grep $host | rev | cut -d\  -f 2 | rev)
                exist1=`ssh $server "ls $homedir/$host/home/ | grep -c $username"`

                if [ $exist1 -ge 1 ]
                then
                        echo ""
                        echo "[Restore Dates]"
                        echo "----------------------------------------------------------------"
                        ssh $server "rdiff-backup -l $homedir/$host/home/$username"
                while [ ! $date ]
                do
                                echo "----------------------------------------------------------------"
                                echo -n "Pick a Restore (incremental): "
                                read date
                                echo "----------------------------------------------------------------"
                done
                        ssh $server "rdiff-backup --force -v5 $homedir/$host/home/$username/rdiff-backup-data/$date $host::/home/$username"
                else
                        echo ""
                        echo "----------------------------------------------------------------"
                        echo "Error: Backup for $username does not exist on $server. Do you want to try an alternate server?"
                        echo " 1) Yes "
                        echo " 2) No "
                        echo "----------------------------------------------------------------"
                        echo -n "Enter Option: "
                        read alt1
                fi

See, at the bottom I want it to read alt1 and go to a specified point, while loop doesn't seem to work and errors out. Maybe I can use goto, I know it's not recommended, but can someone show how if this is the only option. Thanks for the help.

Hobbletoe 04-24-2006 12:46 PM

Do you want to wrap all of your code in a giant while loop?

Code:

alt1=Y

while [ "$alt1" == "Y" ]
do
  alt1=N
  <all of your code>

done

That way, it falls into your while loop the first time through, and sets it up to fall out. But, when they hit that if at the bottom, they can change it to where it will loop if that is what they want. I think that is what you are wanting.

FirmbIT 04-24-2006 01:06 PM

That works, thanks! I wasn't using my while loop correctly.


All times are GMT -5. The time now is 09:54 AM.