I am new to scripting and i am trying to use below script to copy code to multiple servers and multiple locations on each server. the script is not working or doesnt give any error. Any help is appreciated. basically i want a script to get the code from a location (dir below) and read the serverlist file (it has multiple servers and multiple locations on each server ) and copy code to all those servers and locations mentioned on serverlist file. Please help ...
#
# Edit desst variable with directory path where you want file to be copied.
#
HOSTS=/opt/serverlist
dir=/usr/newcode/
echo "Are you sure you want to copy $dir to the standard list of servers"
echo "This may take a while!!"
echo -n "Enter 'y' or 'n':"
read CHOICE
case "$CHOICE" in
y|yes|Yes) while read line
do
serverName=echo $line | awk -F':' '{print $1}'
distLocations = echo $line | awk -F':' '{print $2}'
done
for distLocations in ${distLocations//:/ } ;
do
scp -rp $dir $serverName:$distLocations
done < $HOSTS
;;
*) echo "wrong entry"
;;
esac
(
IFS="$IFS;~"
while read s a b
do
(
scp -r a s:b
echo scp exit code: $?
) 2>&1 | sed "s|^|$a=>$s:$b |" &
done <serverlist ) 2>&1 | tee logfile
Add your separators to $IFS for read, read the file line by line into three variables, copy in the background, and use the stdout/stderr of all the bg copies as both a log and a test of their exiting, prefix every log line by the serverlist line info.
Some ssh installs I have known barf when you spawn too many ssh operations too often or at once. You can search the site for 'bctl' to see how to control the parallelism, and add sleep if necessary to keep the peace.
Indenting sometimes helps in reading / understanding pieces of code.
See some comments on your snippet:
case "$CHOICE" in
y|yes|Yes) while read line
do serverName=echo $line | awk -F':' '{print $1}' # there's not a single ':' in $line; do you mean ';'?
distLocations = echo $line | awk -F':' '{print $2}' # same
done # no redirection: reads from stdin
for distLocations in ${distLocations//:/ } ; # may work, but do you really want use the identical variable name?
# and, sure you want to use the colon?
# I see tildes separating the locations in your file
do scp -rp $dir $serverName:$distLocations
done < $HOSTS # this redirection does not do anything as there's no ready
;;
*) echo "wrong entry"
;;
esac