PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Script for uploading files - use select or case?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Script for uploading files - use select or case?

Réponse
 
LinkBack Outils de la discussion
Vieux 30/05/2007, 06h56   #1
mowgli
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Script for uploading files - use select or case?

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
Vieux 30/05/2007, 09h57   #2
SiKing
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Script for uploading files - use select or case?

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------
  Réponse avec citation
Vieux 31/05/2007, 11h44   #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
Vieux 02/06/2007, 00h59   #4
mowgli
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Script for uploading files - use select or case?

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


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h33.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15937 seconds with 12 queries