LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-24-2006, 12:08 PM   #1
FirmbIT
LQ Newbie
 
Registered: Oct 2005
Posts: 27

Rep: Reputation: 15
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.

Last edited by FirmbIT; 04-24-2006 at 12:14 PM.
 
Old 04-24-2006, 12:46 PM   #2
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
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.

Last edited by Hobbletoe; 04-24-2006 at 01:31 PM.
 
Old 04-24-2006, 01:06 PM   #3
FirmbIT
LQ Newbie
 
Registered: Oct 2005
Posts: 27

Original Poster
Rep: Reputation: 15
That works, thanks! I wasn't using my while loop correctly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Bash function mijohnst Programming 13 10-23-2005 12:40 PM
Bash function pazvant Linux - Software 1 03-08-2005 07:47 AM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration