Operating System - HP-UX
1833756 Members
2685 Online
110063 Solutions
New Discussion

Re: simple test in a script

 
SOLVED
Go to solution
Terrence
Regular Advisor

simple test in a script

I have a portion of a script that needs a test statement to validate that a file has something in it and is not empty. I compare a file to an earlier version of itself to make sure it's no longer growing. However if the file is at zero to start and stays that way for too long the script will assume it's no longer growing and will continue. I want the script to loop back to the compare if it finds the file is empty. I know this is easy but I barely know how to script.

until cmp -s $SOURCE /var/STOCK_ARCHIVE/$SOURCE

do
cp -p $SOURCE /var/STOCK_ARCHIVE/
sleep 300
done
6 REPLIES 6
Patrick Wallek
Honored Contributor

Re: simple test in a script

To test for file existance and non-zero size do:

if [ -s /dir/filename ] ; then
do something
else
doe something else
fi


'man test' for more information and other options.
Gary L. Paveza, Jr.
Trusted Contributor

Re: simple test in a script

Use the -s test

if [ -s /var/STOCK_ARCHIVE/${SOURCE} ]
then
file is not empty
else
file is empty
fi
Terrence
Regular Advisor

Re: simple test in a script

But how do i put this test after my until, do done so that if the file is empty, it goes back to the until do done?

I need to loop it back.
Robert Bennett_3
Respected Contributor

Re: simple test in a script

Here's some little ditty for your notes on test conditions.

The test condition or [condition] - (K) = Korn shell specific
Testing Files
-a file file exists (K)
-b file file exists and is a block special file.
-c file file exists and is a character special file
-d file file exists and is a directory
-e file true if the file exists
-f file file exists and is a regular file
-G file file exists and its group is the effective group ID. (K)
-g file file exists and its set-group-id bit is set
-k file file exists and its sticky bit is set.
-L file file exists and is a symbolic link (K)
-O file file exists and its owner is the effective user ID (K)
-o c Option c is on (K)
-p file file exists and is a named pipe (fifo)
-r file file exists and is readable
-S file file exists and is a socket (K)
-s file file exists and has a size grreater than zero
-t [n] The open file descriptor n is associated with a terminal device; default n is 1
-u file file exists and its set-user-id bit is set
-w file file exists and is writable
-x file file exists and is executable
f1 -ef f2 files f1 and f2 are linked (refer to same file) (K)
f1 -nt f2 file f1 is newer than f2 (K)
f1 -ot f2 file f1 is older than f2 (K)
"All there is to thinking is seeing something noticeable which makes you see something you weren't noticing which makes you see something that isn't even visible." - Norman Maclean
Steve Steel
Honored Contributor
Solution

Re: simple test in a script

Hi


www.shelldorado.com will help


Also put a while loop round your loop

while [ ! -s "/tmp/toto" ]
do
ll -d /tmp/toto
echo lala
sleep 30
done


As long as /tmp/toto is zero length loop continues


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Robert Bennett_3
Respected Contributor

Re: simple test in a script

Thanks Steve - shelldorado.com just went into my favorites....nice site.

B
"All there is to thinking is seeing something noticeable which makes you see something you weren't noticing which makes you see something that isn't even visible." - Norman Maclean