views:

72

answers:

5

I'm not sure why this code isn't working. Its not going to the copy command. I successfully run this manually on the command line (without the check) I don't think i'm performing a correct file check? Is there a better, cleaner way to write this? I just want to make sure the file exists, if so, copy it over. Thanks.

#!/bin/bash
if [ $# != 1 ]; then
    echo "Usage: getcnf.sh <remote-host>" 2>&1
    exit 1
fi

#Declare variables
HOURDATE=`date '+%Y%m%d%H%M'`
STAMP=`date '+%Y%m%d-%H:%M'`
REMOTE_MYCNF=/var/log/mysoft/mysoft.log
BACKUP_DIR=/home/mysql/dev/logs/
export REMOTE_MYCNF HOURDATE STAMP

#Copy file over
echo "Checking for mysoft.log file $REMOTE_MYCNF $STAMP" 2>&1
if [ -f $REMOTE_MYCNF ]; then
echo "File exists lets bring a copy over...." 2>&1
   /usr/bin/scp $1:$REMOTE_MYCNF $BACKUP_DIR$1.mysoft.log
echo "END CP" 2>&1
   exit 0
   else
        echo "Unable to get file" 2>&1
        exit 0
fi
+1  A: 

use sh -x script.sh to see what is happening.

Paul Creasey
thanks, its going "Unable to get file", so its not even hitting the scp. This works if I remove the entire if statement all together. So does that mean, my if statement is not constructed correctly??
jda6one9
It should show the if statement with the variables substituted, check that it is ok and head the file it shows.
Paul Creasey
@Paul - thanks for the debug help.
jda6one9
A: 

You are testing for the existence of a remote file

$1:$REMOTE_MYCNF

using the local name $REMOTE_MYCNF. The if test is never satisfied.

msw
+2  A: 

your checking existing file on remote computer seems like: you should do:

ssh $host "test -f $file"
if [ $? = 0 ]; then
aaa
Thanks for your help. I got it to work this way. Why doesn't like it if its written like this? Is it the spacing? `if [$? =0]; then`
jda6one9
@jda `[ ` is command, `[$?` is not
aaa
A: 

You don't check that $1 is set.

Your file check runs on the local machine - not on the remote.

Jonathan Leffler
A: 

Change your if to:

if[! -f $REMOTE_MYCNF -o ! -d $REMOTE_MYCNF]; 
Chris
@Chris - thanks. this works.
jda6one9
this will work but is not correct, see aaa's answer. Also, -e checks for files and dirs so no need for this or.
Paul Creasey