Afficher un message
Vieux 31/05/2007, 12h44   #3
Brian Mac
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Script for uploading files - use select or case?

hi mowgli,

if you would like to back up a list of files to all servers in a set,
i would recommend using a for loop. you would be able to have the
same command run against all three machines. your statement would
need to be more complex than might be required for a simple for loop
to account for the differences in machine - you could set different
variables based upon the machine that is current in the iteration.
this is where the case statement might come in handy.

if, however, you would like a menu-driven interface to select one
machine to back up files to, you might want to use the select loop
that uses a case statement as a body. here is a sample (works in korn
shell so may need to be modified):

PS3="Please enter a valid number to choose a fruit or to exit (Return
to redraw menu): "
select fruit in apple orange pineapple
do
case $REPLY in
1) echo "You selected apple" ; break ;;
2) echo "You selected orange" ; break ;;
3) echo "You selected pineapple"; break ;;
4) echo "You seleced exit"; exit 0 ;;
*) echo "Invalid selection"
esac
done

the select loop will take care of a lot of the menu wiring for you...
i agree with SiKing - i would get to a library / bookstore and pick up
a shell book. oreilly has a book titled classic shell scripting
book. also there's chris f.a. johnson's book published by apress -
Shell Scripting Recipes: A Problem-Solution Approach.

hope this s,
brian

On May 30, 1:56 am, mowgli <knowledgel...@gmail.com> wrote:
> I tried to make the following script to upload my backed up files to a
> list of servers. Firstly, if I use continue to redisplay the list, I
> get an error which says continue can only be used in while and until
> loops.
>
> Sorry that me being a newbie, it's not very clear to me yet when to
> use select and when to use case.
>
> Also what needs to be done if my username on one server is different
> than on others? And also the ssh port for one server is different?
>
> Here's the script:
>
> #!/bin/bash
> # Purpose: To upload my already backed up files to servers
> # of my choice.
>
> # Set logfile
> LOGFILE=~/backup/`date +%d-%b-%y_%I-%M`.log
>
> # Change default PS3 prompt
> PS3='Select the server to upload backups to: '
>
> # Set path to the files to upload
> FILESPATH=~/backup/*.bz2
> SERVER1=myserver1.example.com
> SERVER2=myserver2.example.com
> SERVER3=myserver3.example.com
> SERVER4=myserver4.example.com
> SERVER5=myserver5.example.com
>
> select SERVER in $SERVER1 $SERVER2 $SERVER3
> do
> echo Now uploading files up files to $SERVER
> scp $FILESPATH user@$SERVER:~/mybackup &>> $LOGFILE
> continue
>
> done
>
> This is the same script but using case statements:
>
> #!/bin/bash
> FILESPATH=~/backup/*.bz2
> SERVER1=myserver1.example.com
> SERVER2=myserver2.example.com
> SERVER3=myserver3.example.com
> SERVER4=myserver4.example.com
> SERVER5=myserver5.example.com
>
> tput clear
> tput bold
> tput cup 20
> echo "Please Select a server to upload the files to:"
> echo 1. Server 1
> echo 2. Server 2
> echo 3. Server 3
> echo 4. Server 4
> echo 5. Server 5
> echo 6. Exit
>
> tput bold
> read SERVER
> case $SERVER in
>
> 1)
> echo "Now uploading backed up files to Server 1"
> #scp -P 22 $FILESPATH user@$SERVER:~/mybackup
> ;;
>
> 2)
> echo "Now uploading backed up filed to Server 2"
> #scp -P 22 $FILESPATH user@$SERVER:~/mybackup
> ;;
>
> 3)
> echo "Now uploading backed up filed to Server 3"
> #scp -P 22 $FILESPATH user@$SERVER:~/mybackup
> ;;
>
> 4)
> echo "Now uploading backed up filed to Server 4"
> #scp -P 22 $FILESPATH user@$SERVER:~/mybackup
> ;;
>
> 5)
> echo "Now uploading backed up filed to Server 5"
> #scp -P 22 $FILESPATH user@$SERVER:~/mybackup
> ;;
>
> 6)
> exit 0
> ;;
>
> *)
> echo "You selected an invalid server. Please try again"
> continue
> ;;
> esac
>
> Regards,
> mowgli



  Réponse avec citation
 
Page generated in 0,07549 seconds with 9 queries