LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-27-2012, 01:55 AM   #1
Vi_rod
Member
 
Registered: Dec 2011
Posts: 42

Rep: Reputation: Disabled
Interactive script for copying files from one location to another


Hi,
I am writing an interactive script for tar-ing a folder and copying it to another location for backup (/opt/backup) and the elif loop used is not ending after executing the contents once, it becomes an infinite loop. What could the error be? please suggest..
Code:
The script -
#!/bin/bash
PS3="Choose (1-3):"
echo "Choose from the list below."
select name in A B C
do
echo "You chose $name."
break
done
guess=$name
while [ "$guess" = "$name" ]; do
if [ "$name" = "" ]; then
                echo "Please choose from the list above."
 elif [ "$name" = "A" ]; then
                tar -cvf ire-l_$Date.tar.gz /opt/webserver/tomcat_ica/webapps/ire-l
		mv /opt/webserver/tomcat_ica/webapps/ire-l_$Date.tar.gz /opt/s2g/backup
		cp /opt/webserver/tomcat_ica/webapps/ire-l/WEB-INF/classes/FileSrPath.properties /opt/backup/property_files/FileSrPath.properties
		cp /opt/webserver/tomcat_ica/webapps/ire-l/WEB-INF/classes/DBCON.properties /opt/backup/property_files/DBCON.properties
		cd /opt/webserver/tomcat_ica/webapps/
		rm -rf ire-l	
		scp 192.168.2.104:/opt/webserver/tomcat_test/webapps/ire-l.tgz /opt/webserver/tomcat_ica/webapps/
		tar -zxvf ire-l.tar.gz
		cp /opt/backup/property_files/FileSrPath.properties /opt/webserver/tomcat_ica/webapps/ire-l/WEB-INF/classes/
		cp /opt/backup/property_files/DBCON.properties /opt/webserver/tomcat_ica/webapps/ire-l/WEB-INF/classes/
        
	elif [ "$name" = "B" ]; then
                tar -cvf ire-a_$Date.tar.gz /opt/webserver/tomcat_icb/webapps/ire-a
		mv /opt/webserver/tomcat_icb/webapps/ire-a_$Date.tar.gz /opt/s2g/backup
		cp /opt/webserver/tomcat_icb/webapps/ire-a/WEB-INF/classes/FileSrPath.properties /opt/backup/property_files/FileSrPath.properties
		cp /opt/webserver/tomcat_icb/webapps/ire-a/WEB-INF/classes/DBCON.properties /opt/backup/property_files/DBCON.properties
		cd /opt/webserver/tomcat_icb/webapps/
		rm -rf ire-a	
		scp 192.168.2.104:/opt/webserver/tomcat_test/webapps/ire-a.tgz /opt/webserver/tomcat_icb/webapps/
		tar -zxvf ire-a.tar.gz
		cp /opt/backup/property_files/FileSrPath.properties /opt/webserver/tomcat_icb/webapps/ire-a/WEB-INF/classes/
		cp /opt/backup/property_files/DBCON.properties /opt/webserver/tomcat_icb/webapps/ire-a/WEB-INF/classes/
	
        elif [ "$name" = "C" ]; then
               tar -cvf ire-l_$Date.tar.gz /opt/webserver/tomcat_ica/webapps/ire-l
		mv /opt/webserver/tomcat_icc/webapps/ire-c_$Date.tar.gz /opt/s2g/backup
		cp /opt/webserver/tomcat_icc/webapps/ire-c/WEB-INF/classes/FileSrPath.properties /opt/backup/property_files/FileSrPath.properties
		cp /opt/webserver/tomcat_icc/webapps/ire-c/WEB-INF/classes/DBCON.properties /opt/backup/property_files/DBCON.properties
		cd /opt/webserver/tomcat_icc/webapps/
		rm -rf ire-c	
		scp 192.168.2.104:/opt/webserver/tomcat_test/webapps/ire-c.tgz /opt/webserver/tomcat_icc/webapps/
		tar -zxvf ire-l.tar.gz
		cp /opt/backup/property_files/FileSrPath.properties /opt/webserver/tomcat_icc/webapps/ire-c/WEB-INF/classes/
		cp /opt/backup/property_files/DBCON.properties /opt/webserver/tomcat_icc/webapps/ire-c/WEB-INF/classes/
       else
                echo -e "\aYes! $guess wrong"
        fi
done
exit

Last edited by Vi_rod; 03-27-2012 at 02:25 AM.
 
Old 03-27-2012, 02:11 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Whenever you post a script, command output, or similar text, it's useful to readers to place that text between code tags. For instance, if you were to type in your post/response/message:

[code]This is standard width text.
It's easier to read code/output with[/code]

You would get a box like below:
Code:
This is standard width text.
It's easier to read code/output with
Doing so will make your question much more answer-receiving friendly.

To your question... Your loop is started with:
Code:
while [ "$guess" = "$name" ]; do
Therefore, your loop will continue to execute for so long as $guess equals $name. Once your script enters the loop, I do not see a change to $guess nor $name. If neither of those variables change, your loop will never terminate, because the condition is always true.

EDIT:
As a side note, the structure of your script does not match what you're (probably) aiming for. My guess is your intent is to loop around the menu-input sequence until the user selects a valid option. Then process the selection--knowing that the input is valid. If so, your loop is in the wrong place.

EDIT2:
My mistake... the bash man page says that the select construct repeatedly asks for valid input. So you can probably safely ignore my earlier edit.

Last edited by Dark_Helmet; 03-27-2012 at 02:27 AM.
 
Old 03-27-2012, 02:40 AM   #3
Vi_rod
Member
 
Registered: Dec 2011
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Dark_Helmet View Post
Whenever you post a script, command output, or similar text, it's useful to readers to place that text between code tags. For instance, if you were to type in your post/response/message:

[code]This is standard width text.
It's easier to read code/output with[/code]

You would get a box like below:
Code:
This is standard width text.
It's easier to read code/output with
Doing so will make your question much more answer-receiving friendly.

To your question... Your loop is started with:
Code:
while [ "$guess" = "$name" ]; do
Therefore, your loop will continue to execute for so long as $guess equals $name. Once your script enters the loop, I do not see a change to $guess nor $name. If neither of those variables change, your loop will never terminate, because the condition is always true.

EDIT:
As a side note, the structure of your script does not match what you're (probably) aiming for. My guess is your intent is to loop around the menu-input sequence until the user selects a valid option. Then process the selection--knowing that the input is valid. If so, your loop is in the wrong place.
Thanks!! I ve edited my previous message with .

and i ve edited
Code:
if [ "$name" = "" ]; then
                echo "Please enter a number."
and placed it outside the while loop.

but what should I edit the
Code:
 while [ "$guess" = "$name" ]; do
part as ??
 
Old 03-27-2012, 02:46 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
It depends on what you want the script to do.

If you just want to try what you have, one solution is to put:
Code:
name="nomatch"
at the end of each of your A, B, and C elif blocks. That will cause the condition to loop condition to fail, which causes the script to exit the loop, and then the script will execute exit and come to an end.
 
Old 03-27-2012, 03:04 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
If you intend to run it only once, then remove the last while and done pair.

OK
 
Old 03-27-2012, 03:23 AM   #6
Vi_rod
Member
 
Registered: Dec 2011
Posts: 42

Original Poster
Rep: Reputation: Disabled
Thanks! Dark_Helmet, AnanthaP. It works when I add name="nomatch"
 
Old 03-27-2012, 03:27 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by Dark_Helmet
EDIT2:
My mistake... the bash man page says that the select construct repeatedly asks for valid input. So you can probably safely ignore my earlier edit.
Actually you were right the first time as his select has a break in it no matter the input, ie. if the user presses '4' it will leave the loop and 'name' will have nothing in it.

@Vi_rod - placing the if outside the loop will also not help you as once you enter another number, correct or not, 'name' will still not be populated and the issue continues.

I would suggest placing your 'if' in the select loop, something like:
Code:
select name in A B C
do
    if [[ $name == [ABC] ]]
    then
	echo "You chose $name."
	break
    else
	echo "You have entered an invalid choice, please try again."
    fi
done
 
  


Reply



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
copying only new file to other location ananthkadalur Linux - Newbie 7 07-20-2011 06:29 PM
how do i write a bash script to restore deleted files to original location? BrinkofMadness Linux - Newbie 8 12-05-2010 06:04 PM
[SOLVED] I need a shell script to delete files wich location should be read from a log file jorgemarmo Programming 24 07-02-2010 07:07 AM
BASH - How to open an interactive script from a non interactive script..... OldGaf Programming 4 06-29-2008 04:34 PM
AWK script: moving, copying and renaming files uprjamaja Programming 10 12-05-2006 01:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:53 AM.

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