views:

51

answers:

1

Need some shell scripting help, especially with my if-then-else logic. I want to combine both conditions, but not sure if the file checks will work the same? Should I be doing something like a nested if?? My script uses the if statements to do file checks to see if they exist, then do something..

There is probably a better way to do file checks part too.

Any help, critique would be appreciated. Thanks.

Here's my code, it sort of works.

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

#Declare variables
STAMP=`date '+%Y%m%d-%H:%M'`
REMOTE_MYCNF=/var/log/mysoft/mysoft.log
REMOTE_GZ=/var/log/mysoft/mysoft.log.1.gz
REMOTE_DIR=/var/log/mysoft/
BACKUP_DIR=/home/mysql/dev/logs/
NEWLOG="foo-temp.log"
export REMOTE_MYCNF STAMP SHORTNAME
export REMOTE_DIR REMOTE_GZ

#Copy file over
echo "START..." 2>&1
test -f $BACKUP_DIR$1.mysoft.log
if [ $? = 0 ]; then
   echo "Local log file exists, clean up for new copy..." 2>&1
   /bin/rm $BACKUP_DIR$1.mysoft.log
   exit 0
   else
        echo "File does not exist, getting a new copy..." 2>&1
fi

echo "Checking remotely in $1 for foo logfile $REMOTE_MYCNF $STAMP" 2>&1
if [ ! -f $REMOTE_MYCNF ]; then
   echo "File exists remotely, creating new logfile and copy here...." 2>&1
   ssh $1 "zcat $REMOTE_GZ >> $REMOTE_DIR$NEWLOG"
   ssh $1 "cat $REMOTE_MYCNF >> $REMOTE_DIR$NEWLOG"
   /usr/bin/scp $1:$REMOTE_DIR$NEWLOG $BACKUP_DIR$1.mysoft.log
   echo "end remote copy" 2>&1
   echo "Cleaning up remote files" 2>&1
   ssh $1 "rm $REMOTE_DIR$NEWLOG"
   exit 0
   else
        echo "Unable to get file" 2>&1
        exit 0
fi

Updated code using help:

    if [ -f $BACKUP_DIR$1.mysoft.log ]; then
       echo "Local log file exists, clean up for new copy..." 2>&1
       /bin/rm $BACKUP_DIR$1.mysoft.log
       exit 0
    else
        echo "File does not exist, getting a new copy..." 2>&1
        echo "Checking remotely in $1 for foo logfile $REMOTE_MYCNF $STAMP" 2>&1
               if [ ! -f $REMOTE_MYCNF ]; then
                    echo "File exists remotely, creating new logfile and bring a copy here...." 2>&1
                    ssh $1 "zcat $REMOTE_GZ >> $REMOTE_DIR$NEWLOG"
                    ssh $1 "cat $REMOTE_MYCNF >> $REMOTE_DIR$NEWLOG"
                    /usr/bin/scp $1:$REMOTE_DIR$NEWLOG $BACKUP_DIR$1.mysoft.log
                    echo "end remote copy" 2>&1
                    echo "Cleaning up remote files" 2>&1
                    ssh $1 "rm $REMOTE_DIR$NEWLOG"
                    exit 0
               else
                    echo "Unable to get file" 2>&1
                    exit 0


         fi
fi
+2  A: 

The file test can be combined into one statement like this:

if [ -f $BACKUP_DIR$1.mysoft.log ]; then

At a glance, it doesn't look like you need to export any of the variables.

If you intend for the if [ ! -f $REMOTE_MYCNF ]; then block to be executed within the else of the previous if, just move it within it.

if ...
then
   foo
else
    if ...
    then
        bar
    else
        baz
    fi
fi

If you need to check two things:

if [ "$foo" = "bar" && "$baz" = "qux" ]

Always quote your variables.

In a short script it's fine to use positional parameters such as $1 directly, but it makes a longer script easier to understand if a variables with meaningful names are assigned their values near the top.

remote_host=$1

When you want to echo errors to stderr do it this way:

echo "Message" >&2

The way you have it, you're echoing the message and any errors the echo itself may produce (pretty rare) to stdout.

Dennis Williamson
@Dennis W. - thanks for your help. I made some changes based on your suggestion (see update code above). I'm have a problem with my current logic though, especially in the if statement. If the local file exists, I want to remove that file and then bring a fresh copy of the remote file over. What happens, when I run in the first time, it will remove the file and get out completely. Next time I run it again, it works fine, cause the local file exists which is ok..but I want it always bring a fresh copy from the remote server..if i'm making sense. thanks.
jda6one9
@jda6one9: Then your `if` blocks should not be nested. The structure should be more like the first version. Also, something I missed on my first read is that you shouldn't have `exit` unless there's an error or you have some reason to exit the script. The way you have it now, it exits after any operation. The one after the removal of the local file is the one that is causing the problem. In general, the fewer paths into a program and out of a program (or script, function, etc.) the better (some people insist on one way in and one way out).
Dennis Williamson
@Dennis - Understood. Thanks! It works fine now.
jda6one9
@Dennis: you tell always to quote variables and then do the same mistake in `remote_host=$1`
Roman Cheplyaka
@Roman: You usually don't have to quote variables in an assignment since word splitting is not done at that time.
Dennis Williamson
@Dennis: you're right, my mistake, sorry.
Roman Cheplyaka