Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF script using OR

Status
Not open for further replies.

mmcds

MIS
Jul 5, 2007
28
US
This may be pretty easy, but I am having trouble with it. I have an IF statement that I want to do and I want it to check to see if 4 different files are nonzero byte files. below is my if statement.

if [ -s /nfslogs/weblogs/access_log.`date +"%m%d"` ] ; then

How can I have it look at 4 files on the same IF statement? I tried separating it with a || but that didn't seem to work. I want to look at these files.

/nfslogs/weblogs/cona/agent_log.`date +"%m%d"`
/nfslogs/weblogs/cona/access_log.`date +"%m%d"`
/nfslogs/weblogs/cona/test_log.`date +"%m%d"`
/nfslogs/weblogs/cona/referer_log.`date +"%m%d"`


 
man test

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This IBM web page will give you all you want to know but, if you combine too much your script becomes unweildy

Consider
Code:
PATH=/nfslogs/weblogs/cona
PSCRIPT=_log.$(date +%m%d)
if [ -s ${PATH}agent$PSCRIPT || -s ${PATH}access$PSCRIPT || -s  ${PATH}test$PSCRIPT || ${PATH}referer$PSCRIPT ]
then
  echo one of the files has non zero length
fi
- what a nightmare to work on as the if statement goes over 80 chars!

Wouldnt it be better to do
Code:
PATH=/nfslogs/weblogs/cona
PSCRIPT=_log.$(date +%m%d)
for file in agent access test referer
do
  [[ -s $PATH$file$PSCRIPT ]] && echo $PATH$file$PSCRIPT has non zero length
done

Ceci n'est pas une signature
Columb Healy
 
Columb, the test command use -o as OR operator (not ||)
 
PHV
Ooops - getting confused with the [[ expression || expression ]] construct - sorry all.

Ceci n'est pas une signature
Columb Healy
 
That has worked your suggestions, but now say I have /nfslogs/weblogs and about 100 different subdirectories under there that all have their own agent, access, test, referer files I want to check for. How could I easily scan through multiple directories instead of have a PATH1=/nfslogs/weblogs/cona PATH2=/nfslogs/weblogs/conb, etc.
 
You could put you existing 'for loop' inside another one, after specifying a list of directories to process, eg:
Code:
   list_of_dirs="/path/to/dir1 /path/to/dir2 /path/to/dir3"

   for this_path in ${list_of_dirs}
   do
       PATH=${this_path}
       .
       .
       for file in agent access test referer
       do
       <test for files with non zero length>
       done
   done

Alternatively put the list of directories into a file (one on each line) and start the above loop with:

for this_path in `cat filenamelist.txt`
do
. . . . .


I hope that helps.

Mike
 
Oh no, Mike, you'll awaken the UUOC police!!

I want to be good, is that not enough?
 
Hi Ken,

Frankly, I'm not really bothered. I have broad shoulders. I would rather write code that reads from left to right & down the page and is easy to understand and maintain by less experienced programmers. I don't want to be looking for input to a loop at the end of the loop (especially if it is on the next screen or page). And to those people who spend ages 'optimising' code to save a few nanoseconds I would say that I have a life outside of work.

I hope that helps

Mike
 
Seems a sensible enough approach to me!

I want to be good, is that not enough?
 
The suggestions are great everybody. Thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top