LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-23-2014, 05:47 AM   #1
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 245

Rep: Reputation: 4
bash script to check for multiple file existence and then scp to another server


I have two logs locations
/opt/apache-tomcat-7.0.34/logs/
/opt/apache-tomcat-7.0.34/another-logs/


Log format in /opt/apache-tomcat-7.0.34/logs/ is

localhost.2014-01-22.log
catalina.2014-01-22.log
catalina-2014-01-22.out
localhost_access_log.2014-01-22.txt


Log format in /opt/apache-tomcat-7.0.34/another-logs/ is

process.2014-01-20.log
qwerty.2014-01-20.log
asdfgh.2014-01-20.log
fgghghgh.2014-01-20.log
ghghghghghghgh.2014-01-20.log
ghghghghg.2014-01-20.log
nbnbnb.2014-01-20.log


currently I have a script to scp all these logs to another server... Now I need to check whether the logs are present if not it should give o/p as 0. If yes it should scp the files....



I tried like

DATE=$(date -d "-1 day" +%Y-%m-%d)
cd /opt/apache-tomcat-7.0.34/logs/
if [[ -f *$DATE* ]]; then
echo "0"
else
echo "1"

But I get 1 as o/p, but files are present......


Is there an way to check for mutliple file existence using multiple wild cards, or is there any other way I can work it out
 
Old 01-23-2014, 07:39 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Can you discriminate using ls?
Code:
~/testcode$ ls *.phy
ls: cannot access *.phy: No such file or directory
~/testcode$ echo $?
2
Code:
~/testcode$ ls *.log
data-2.log  data.log
~/testcode$ echo $?
0

Last edited by rtmistler; 01-23-2014 at 07:41 AM.
 
Old 01-24-2014, 12:07 AM   #3
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 245

Original Poster
Rep: Reputation: 4
i figured it out

#!/bin/sh
echo "SCRIPT STARTED"
DATE=$(date -d "-1 day" +%Y-%m-%d)
MONTH=$(date +%B-%y)
####Check logs existence
find /opt/apache-tomcat-7.0.34/logs/ -type f -name *$DATE* -exec ls -ltr {} \;
a=`echo $?`
find /opt/apache-tomcat-7.0.34/another-logs/ -type f -name *$DATE* -exec ls -ltr {} \;
b=`echo $?`
if [[ ! $a = 0 || ! $b = 0 ]]; then
echo "Failure"
flag=0
else
echo "Success"
flag=1
fi

if ssh admin@*.*.*.* [ -d /Test/$HOSTNAME/$MONTH ]; then
scp /opt/apache-tomcat-7.0.34/logs/*$DATE* admin@*.*.*.*:/Test/$HOSTNAME/$MONTH/
scp /opt/apache-tomcat-7.0.34/another-logs/*$DATE* admin@*.*.*.*:/Test/$HOSTNAME/$MONTH/
ssh admin@*.*.*.* gzip /Test/$HOSTNAME/$MONTH/*
d=`echo $?`
if [[ $d = 1 ]]; then
echo "Failure"
flag=0
else
echo "Success"
flag=1
fi

else
ssh admin@*.*.*.* mkdir -p /Test/$HOSTNAME/$MONTH
scp /opt/apache-tomcat-7.0.34/logs/*$DATE* admin@*.*.*.*:/Test/$HOSTNAME/$MONTH/
scp /opt/apache-tomcat-7.0.34/anothoer-logs/*$DATE* admin@*.*.*.*:/Test/$HOSTNAME/$MONTH/
ssh admin@*.*.*.* gzip /Test/$HOSTNAME/$MONTH/*
e=`echo $?`
if [[ $e = 1 ]]; then
echo "Failure"
flag=0
else
echo "Success"
flag=1
fi

fi
echo "SCRIPT COMPLETED"
# FLAG TO RETURN 0 for FAILURE, 1 for SUCCESS
echo $flag
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting help. Check for file/directory existence diamond_D Programming 17 08-01-2012 02:44 PM
Multiple scp/ssh in one bash script Lovelyhard Programming 13 07-31-2012 09:37 AM
[SOLVED] Need bash script to check existence of images within an HTML document thunor Programming 7 10-26-2010 10:20 AM
[bash] Remote file existence check danket Linux - Newbie 5 01-14-2008 04:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration