The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Delete an empty file from dir. Script40 Shell Programming and Scripting 2 04-02-2008 02:09 PM
The output file is empty. Please Help balzzz UNIX for Dummies Questions & Answers 4 01-31-2008 09:15 PM
empty file in hp-ux Mizi UNIX for Advanced & Expert Users 2 08-09-2006 03:29 AM
Deleting the empty file rkumar28 Shell Programming and Scripting 2 05-03-2006 11:05 AM
File exists and is empty dstinsman UNIX for Dummies Questions & Answers 5 02-09-2006 07:05 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-05-2002
Registered User
 

Join Date: Nov 2001
Posts: 59
Question File is not empty?

How do I check to make sure a file is not zero bytes?

Something like:

if [ !-z $filename ]
then


?????
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-05-2002
Kelam_Magnus's Avatar
Unix does a body good.
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
The test command is your best bet.

Look at the bottom -z is for zero test and -n is for non-zero test.


man test
test(1) test(1)
NAME
test - condition evaluation command
SYNOPSIS
test expr
[ expr ]
DESCRIPTION
The test command evaluates the expression expr and, if its value is
True, returns a zero (true) exit status; otherwise, a nonzero (false)
exit status is returned. test also returns a nonzero exit status if
there are no arguments. The following primitives are used to
construct expr:
-r file True if file exists and is readable.
-w file True if file exists and is writable.
-x file True if file exists and is executable.
-f file True if file exists and is a regular file.
-d file True if file exists and is a directory.
-c file True if file exists and is a character special
file.
-b file True if file exists and is a block special file.
-p file True if file exists and is a named pipe (fifo).
-u file True if file exists and its set-user-ID bit is
set.
-g file True if file exists and its set-group-ID bit is
set.

-k file True if file exists and its sticky bit is set.

-s file True if file exists and has a size greater than
zero.

-h file True if file exists and is a symbolic link.

-t [fildes] True if the open file whose file descriptor number is fildes (1 by default) is associated with a terminal device.

-z s1 True if the length of string s1 is zero.


-n s1 True if the length of the string s1 is non-zero.
__________________
My brain is your brain
Reply With Quote
  #3 (permalink)  
Old 04-06-2002
Jimbo
Guest
 

Posts: n/a
lesstjm is asking how to make sure a file is not zero bytes. I think he wants to test actual file content, in which case he will want the -s option. But since the sample code used a variable, that leaves the door open to the other interpretation of wanting to test content of the variable instead of the file it references.

So just to clarify, -s will test the file itself for content, and -z and -n will test the contents of the variable.
Reply With Quote
  #4 (permalink)  
Old 04-08-2002
Registered User
 

Join Date: Nov 2001
Posts: 59
Talking thank you

I appreciate your help. I tested with the -s and it worked great.
Reply With Quote
  #5 (permalink)  
Old 05-07-2004
oti oti is offline
Registered User
 

Join Date: May 2002
Posts: 14
How can I test if a file is opened by a process?

I have the following situation: a process (suppose I don't know anything about it) writes data into a file very very slowly. I want to copy the file to another location ONLY after the process closes the file (and I want to be very sure about that).

I've tried to check the file size from time to time but this method doesn't always work. I've also tried with
test -N file
where −N file is true if file exists and was modified since last read
but I can't figure out how this works.

Can anyone give a clue?
Thanks!
Reply With Quote
  #6 (permalink)  
Old 05-07-2004
Registered User
 

Join Date: Sep 2003
Posts: 25
Not sure if fuser will work on all flavours of UNIX.

This will loop until the file is copied. It will check every 60 seconds to see if the file is still locked and copy it when it's unlocked.

I haven't tested this

#!/bin/ksh

Status=1

while [ $Status -ne 0 ]
do
sleep 60

ProcID=`/usr/sbin/fuser filename | awk -F: '{print $2}'`

if [ "$ProcID" = "" ]
then
cp filename somewhere
fi
Status = $?

done

########################

Matt.
Reply With Quote
  #7 (permalink)  
Old 05-07-2004
flim flam flamma jamma
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
look into useing lsof.

i have a process that can take about 1 to 2 hours to pump out 1 small file and i had the same issues so i went with lsof.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:08 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0