December 12th, 2014, 04:10 PM
-
Script to Push Files
Hey Guys, Thanks for always being helpful, I have another issue that I need a little insight on how to fix.
See the below script I have and the error I get. I don't understand why it does that, am I not using the continue correctly?
Code:
#!/bin/bash -x
# @(#) File: filepush.sh
# ---------------------------------------------------------------------------
# Modication History:
# Date Name Description
# 12/10/2014 Emmanuel Iroanya Script to copy a file to all hosts listed in serverlist file
# ---------------------------------------------------------------------------
source /opt/mgr/conf/file.conf
source /opt/mgr/conf/dest.conf
HOSTS=/opt/mgr/conf/$1serverlist.conf
echo "Are you sure you want to copy $file to the list of $1 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
ssh "$line" "mkdir -p $dest" && scp -r "$file" "$line:$dest"
done < $HOSTS
continue ;;
n|no|No) echo "Please try again later"
esac
Code:
./filepush: line 30: continue: only meaningful in a `for', `while', or `until' loop
December 12th, 2014, 04:29 PM
-
You're using continue in the case and, as the error message says, you can only use it in a for, while, or until loop.
I'm not sure what it's even supposed to be doing there so... remove it?
December 15th, 2014, 09:02 AM
-
Originally Posted by requinix
You're using continue in the case and, as the error message says, you can only use it in a for, while, or until loop.
I'm not sure what it's even supposed to be doing there so... remove it?
Thanks alot for your input! Great help! I am however running into a new problem when I take out the continue, which is why I put it there in first place.
See below the output after running the script and see the sample conf file that has the servers list.
It seems that it doesn't always go through the entire list of servers, it will start a loop then go halfway, then stop.
CONF FILE:
Code:
cmap01.foobar.com
cmap02.foobar.com
cmap03.foobar.com
cmap05.foobar.com
rfap05.foobar.com
t2000.foobar.com
OUTPUT:
Code:
[sseadmin@cmap04 bin]$ ./filepush nonprod
+ source /opt/mgr/conf/file.conf
++ file=/opt/mgr/tools/jcecheck.sh
+ source /opt/mgr/conf/dest.conf
++ dest=/opt/mgr/tools
+ HOSTS=/opt/mgr/conf/nonprodserverlist.conf
+ echo 'Are you sure you want to copy /opt/mgr/tools/jcecheck.sh to the list of nonprod servers'
Are you sure you want to copy /opt/mgr/tools/jcecheck.sh to the list of nonprod servers
+ echo 'This may take a while!!'
This may take a while!!
+ echo -n 'Enter '\''y'\'' or '\''n'\'':'
Enter 'y' or 'n':+ read CHOICE
y
+ '[' y ']'
+ case "$CHOICE" in
+ read line
+ ssh cmap01.foobar.com 'mkdir -p /opt/mgr/tools'
+ scp -r /opt/mgr/tools/jcecheck.sh cmap01.foobar.com:/opt/mgr/tools
jcecheck.sh 100% 1082 1.1KB/s 00:00
+ read line
+ ssh cmap02.foobar.com 'mkdir -p /opt/mgr/tools'
+ scp -r /opt/mgr/tools/jcecheck.sh cmap02.foobar.com:/opt/mgr/tools
jcecheck.sh 100% 1082 1.1KB/s 00:00
+ read line
+ ssh cmap03.foobar.com 'mkdir -p /opt/mgr/tools'
+ scp -r /opt/mgr/tools/jcecheck.sh cmap03.foobar.com:/opt/mgr/tools
jcecheck.sh 100% 1082 1.1KB/s 00:00
+ read line
+ ssh cmap05.foobar.com 'mkdir -p /opt/mgr/tools'
+ scp -r /opt/mgr/tools/jcecheck.sh cmap05.foobar.com:/opt/mgr/tools
jcecheck.sh 100% 1082 1.1KB/s 00:00
+ read line
+ read -p 'Continue [yN] ? ' CHOICE
Continue [yN] ? y
+ '[' y ']'
+ case "$CHOICE" in
+ read line
+ ssh cmap01.foobar.com 'mkdir -p /opt/mgr/tools'
+ scp -r /opt/mgr/tools/jcecheck.sh cmap01.foobar.com:/opt/mgr/tools
jcecheck.sh 100% 1082 1.1KB/s 00:00
+ read line
+ ssh cmap02.foobar.com 'mkdir -p /opt/mgr/tools'
+ scp -r /opt/mgr/tools/jcecheck.sh cmap02.foobar.com:/opt/mgr/tools
jcecheck.sh 100% 1082 1.1KB/s 00:00
+ read line
+ read -p 'Continue [yN] ? ' CHOICE
Continue [yN] ?
December 15th, 2014, 04:34 PM
-
What's your code now? I don't see an input loop in the original.
December 16th, 2014, 08:16 AM
-
Originally Posted by requinix
What's your code now? I don't see an input loop in the original.
Sorry, I did make a slight change and didn't update code.
Code:
#!/bin/bash -x
# @(#) File: filepush.sh
# ---------------------------------------------------------------------------
# Modication History:
# Date Name Description
# 12/10/2014 Emmanuel Iroanya Script to copy a file to all hosts listed in serverlist file
# ---------------------------------------------------------------------------
source /opt/mgr/conf/file.conf
source /opt/mgr/conf/dest.conf
HOSTS=/opt/mgr/conf/${1}serverlist.conf
echo "Are you sure you want to copy $file to the list of $1 servers"
echo "This may take a while!!"
echo -n "Enter 'y' or 'n':"
read CHOICE
while [ $CHOICE ]
do
case "$CHOICE" in
y|yes|Yes) while read line
do
ssh -n "$line" "mkdir -p $dest" && scp -r "$file" "$line:$dest"
done < $HOSTS ;;
n|no|No) echo "Please try again later"; break ;;
esac
read -p "Continue [yN] ? " CHOICE
done
December 16th, 2014, 10:15 AM
-
Not wholly sure why the inner loop is breaking after the first 4 lines in the host list file, nor why it then breaks after the first 2. But you do realise that the 'want to continue' part will only cause the file to be copied to the target hosts again? In addition, the "while [ $CHOICE ]" loop is not a good thing to have as so long as the value of CHOICE can be evaluated as 'true' (or at least not false) it will continue - and which is truer, 'y' or 'n'? 
If, as I suspect, you want the user to confirm that they do want the file to be copied to all target hosts, then I'd change the outer loop to a simple if statement and, by (personal) preference I'd probably have the inner loop for a "for line in $HOSTS" type construct.
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc