|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
mowgli wrote:
> 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. That's correct. <http://tiswww.case.edu/php/chet/bash/bashref.html#IDX72> From your code, I suspect that what you actually want is a for loop <http://tiswww.case.edu/php/chet/bash/bashref.html#IDX31>: for SERVER in $SERVER1 $SERVER2 $SERVER3 do echo Now uploading files up files to $SERVER scp $FILESPATH user@$SERVER:~/mybackup &>> $LOGFILE # for loop will "continue" for you done > Sorry that me being a newbie, it's not very clear to me yet when to > use select and when to use case. Don't apologize for being a newbie, everyone had to start somewhere. But you might want to pick up a book at you local library on basic programming. 'case' and 'select' are both used for completely different things. case: <http://tiswww.case.edu/php/chet/bash/bashref.html#IDX37> select: <http://tiswww.case.edu/php/chet/bash/bashref.html#IDX40> > 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? You will have to use _different_ variables. Have a look at arrays <http://tiswww.case.edu/php/chet/bash/bashref.html#SEC81>, which could simplify your code a bit. HTH. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GE d+(-) s+: a@ C+ ULAHS++$ P- L+>++ E--- W++ N++ o !K w--(+) O- M?>+ V? PS+ PE+(++) Y+ PGP- t+ 5 X R !tv b+ DI(+) D G e++ h---- r+++@ y++++ ------END GEEK CODE BLOCK------ |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On May 31, 3:44 pm, Brian Mac <mcnamarabr...@gmail.com> wrote:
Thanks Brian for clarification. > 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. I changed the script to this and it's working except in case connection to one of the server fails: #!/bin/bash # Set logfile LOGFILE=~/backup/`date +%d-%b-%y_%I-%M`.log # Bold and centered output on screen #clear tput bold #tput cup 5 # Set path to backup files FILESPATH=~/backup/*.bz2 # Define servers SERVER1=myserver1.example.com SERVER2=myserver2.example.com SERVER3=myserver3.example.com SERVER4=myserver4.example.com #SERVER5=myserver5.example.com # Currently down for SERVER in $SERVER1 $SERVER3 $SERVER4 $SERVER5 do if [ $SERVER = $SERVER4 ] then echo "Now uploading files to $SERVER ..." echo scp $FILESPATH user123@$SERVER:~/backup 2>>$LOGFILE elif [ $SERVER = $SERVER5 ] then echo "Now uploading to $SERVER ..." echo scp -P 700 $FILESPATH user456@$SERVER:~/ backup 2>> $LOGFILE else echo "Now uploading files to $SERVER ..." echo scp $FILESPATH user@$SERVER:~/backup 2>> $LOGFILE fi done > 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): This is what I needed I think. Regards, mowgli |
|
![]() |
| Outils de la discussion | |
|
|