jdamae
jdamae

Reputation: 3909

shell script help - checking for file exists

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

Upvotes: 2

Views: 6982

Answers (5)

Chris
Chris

Reputation: 12078

Change your if to:

if[! -f $REMOTE_MYCNF -o ! -d $REMOTE_MYCNF]; 

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

You don't check that $1 is set.

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

Upvotes: 0

Anycorn
Anycorn

Reputation: 51445

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

ssh $host "test -f $file"
if [ $? = 0 ]; then

Upvotes: 3

msw
msw

Reputation: 43487

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.

Upvotes: 0

Paul Creasey
Paul Creasey

Reputation: 28824

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

Upvotes: 2

Related Questions