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 and shell scripting languages 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 06:09 PM
The output file is empty. Please Help balzzz UNIX for Dummies Questions & Answers 4 02-01-2008 01:15 AM
empty file in hp-ux Mizi UNIX for Advanced & Expert Users 2 08-09-2006 07:29 AM
Deleting the empty file rkumar28 Shell Programming and Scripting 2 05-03-2006 03:05 PM
File exists and is empty dstinsman UNIX for Dummies Questions & Answers 5 02-09-2006 11:05 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-05-2002
lesstjm lesstjm is offline
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


?????
  #2 (permalink)  
Old 04-05-2002
Kelam_Magnus's Avatar
Kelam_Magnus Kelam_Magnus is offline Forum Advisor  
Registered User
  
 

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.
  #3 (permalink)  
Old 04-06-2002
Jimbo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
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.
  #4 (permalink)  
Old 04-08-2002
lesstjm lesstjm is offline
Registered User
  
 

Join Date: Nov 2001
Posts: 59
Talking thank you

I appreciate your help. I tested with the -s and it worked great.
  #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!
  #6 (permalink)  
Old 05-07-2004
butterfm butterfm is offline
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.
  #7 (permalink)  
Old 05-07-2004
Optimus_P Optimus_P is offline Forum Advisor  
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0