Script to Push Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to Push Files
# 1  
Old 12-12-2014
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

# 2  
Old 12-12-2014
Remove the continue and append a ;; after "Please try again later".

As you seem to use indention, the do .. done can be indented too, only the EOF really needs to be at the beginning of the line and have no whitespace after, where as the cat > EOF can be anywhere.

Also, i'm not sure this is doing what you expect:
Code:
HOSTS=/opt/mgr/conf/$1serverlist.conf

You might want to change this to:
Code:
HOSTS=/opt/mgr/conf/${1}serverlist.conf

hth

Last edited by sea; 12-12-2014 at 06:55 PM..
# 3  
Old 12-12-2014
Quote:
Originally Posted by sea
Remove the continue and append a ;; after "Please try again later".

As you seem to use indention, the do .. done can be indented too, only the EOF really needs to be at the beginning of the line and have no whitespace after, where as the cat > EOF can be anywhere.

Also, i'm not sure this is doing what you expect:
Code:
HOSTS=/opt/mgr/conf/$1serverlist.conf

You might want to change this to:
Code:
HOSTS=/opt/mgr/conf/${1}serverlist.conf

hth
When I run the script it looks for a nonprod or prod server list conf file.

I run it like
Code:
  ./filepush nonprod

so it gets the list right.
# 4  
Old 12-12-2014
$1serverlist.conf will look for an empty string -> .conf
As there is no variable named like $1serverlist.

${1}serverlist.conf will look for nonprodserverlist.conf.

hth
# 5  
Old 12-13-2014
Hello gkelly1117

With 'continue' , you may have meant something of this sort...
Code:
#!/bin/sh

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 "$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


Last edited by ongoto; 12-13-2014 at 03:04 AM..
# 6  
Old 12-15-2014
Thanks alot for your input! Great help! I am however running into a new problem with your fix that happened without the continue, which is why I put it 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] ?


Quote:
Originally Posted by ongoto
Hello gkelly1117

With 'continue' , you may have meant something of this sort...
Code:
#!/bin/sh

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 "$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

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on a script to push files once a day

Hello! Please I need some help on writing a script to push files from one server to the other once a day, everyday. I know that I can use this script on a crontab to send the files , but I am not sure how to start writing it, the actual script. I could start by declaring some variables: ... (4 Replies)
Discussion started by: fretagi
4 Replies

2. Homework & Coursework Questions

Need help with a Perl Script using Pop, Shift, & Push

Hello everyone, I am new to Perl and I am having some issues getting a script to work. I have to create a script that uses an array of 52 cards, "shuffles" the cards (using loops with the pop, shift, and push commands), and prints out the top five. This is not a randomizing of the array just a... (2 Replies)
Discussion started by: Hax0rc1ph3r
2 Replies

3. UNIX for Dummies Questions & Answers

Setup a push script from NIS

Hi guys, can we get a pushed script for every NIS client like a policy.? I need to set a symbolic links for for every client. Thanks ... (1 Reply)
Discussion started by: pianz
1 Replies

4. UNIX for Dummies Questions & Answers

Rsync push or pull?

We have a cluster of 3 web servers. I'll be updating a single master server and copying info to the other 2 slave servers. What's the best way of synching all of them? Run rsync on each of the slave servers to pull the updates from the master? Or run rsync on the master to push the updates to the... (1 Reply)
Discussion started by: gaspol
1 Replies

5. Shell Programming and Scripting

push and pop directories and subdirectories

I need to use pushd and popd to navigate all of the subdirectories in my current directory. I know how to get into each subdirectory, add it to the stack, and pop back out, but i cant figure out how to get into subdirectories deeper than the first without adding a foreach and if statement for... (1 Reply)
Discussion started by: ollie88r
1 Replies

6. Shell Programming and Scripting

How can I push an X app to the front by PID?

I'm trying to write a script that will start an X application if it's not already running, but will find it and pop it to the front if it is already running (maybe it's lost on a busy desktop). Is there such a mechanism? Here is part of the logic: VRDP="$(ps ax|grep "rdesktop .*... (3 Replies)
Discussion started by: KenJackson
3 Replies

7. Linux

FTP push

Hi I am trying to send a file form one linux server into an another linux server. I cannot do ftp get. Can anyone please assist me how can I push the file to the other server ? Thanks. (2 Replies)
Discussion started by: sureshcisco
2 Replies
Login or Register to Ask a Question