LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Interactive script for copying files from one location to another (https://www.linuxquestions.org/questions/linux-newbie-8/interactive-script-for-copying-files-from-one-location-to-another-936611/)

Vi_rod 03-27-2012 01:55 AM

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


Dark_Helmet 03-27-2012 02:11 AM

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.

Vi_rod 03-27-2012 02:40 AM

Quote:

Originally Posted by Dark_Helmet (Post 4637311)
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 ??

Dark_Helmet 03-27-2012 02:46 AM

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.

AnanthaP 03-27-2012 03:04 AM

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

OK

Vi_rod 03-27-2012 03:23 AM

Thanks! Dark_Helmet, AnanthaP. It works when I add name="nomatch"

grail 03-27-2012 03:27 AM

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



All times are GMT -5. The time now is 01:57 PM.