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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count files quietly

Status
Not open for further replies.

barryp

Programmer
Jan 30, 2002
48
GB
I only need to execute a shell script IF there are some zip files in my target directory

So I begin with

if `test -r tempdata/*.zip`
then

BUT sometimes get TOO MANY ARGUMENTS for test

So I try

if `ls tempdata/*.zip | wc -l` > 0
then

BUT this fails if there are NO zip files in my directory

There must be an easy way
Someone help a Unix newbie ?
 
Use an exit statement if the condition equals 0 (no zip files). Show us the rest of the script so we can better understand what your trying to do.


ChrisP
 
Thanks for the response

I think its a message from 'test' in example 1 and 'ls' in example 2 which I'm trying to avoid.

If I say

ls xxxx.abc

as part of my IF statement
I get "No such file or directory"
Its these meassges I'm trying to avoid
 
It would be a lot easier if you posted the rest of the script, or at least the statement after 'then'. I'm trying to figure out why your trying to accomplish with this script.

What do you mean by you get "too many arguments" with test?


ChrisP
 
Here's the script

As you can see I'm still a learner !




scp -i ~/.ssh/id_dsa_mfssh bteam@193.xxx.xxx.xxx:F_2003001/export/mf_????????[0-9]*.zip tempdata/.
#if `test -r tempdata/*.zip`
#then
for i in tempdata/*.zip
do
if unzip -t $i 1>/dev/null 2>/dev/null
then
unzip -j $i -d tempdata
file=tempdata/`basename $i .zip`.xml
php -q XML2Compass.php $file
rm $file
mv $i tempdata/done
else
echo "error"
fi
done
#fi
 
I just copied and pasted your script and changed all instances of "tempdata" to /tmp, since tempdata doesn't exist on my system. It ran successfully, and echoed "error" since I didn't have any .zip files in /tmp. Make sure to specify the absolute paths to each directory and make sure they exist before you run the script.

You can also use the exit command in place of "echo error", unless you want to see the message on the terminal.


ChrisP
 
Thanks again for replying

I haven't been very clear

Here is a simple script

for i in tempdata/*.xml
do
php -q XML2Compass.php $i
done

With NO xml files in tempdata the php script is executed with $i set to the string "tempdate/*.xml"

So I get ugly php errors about fopen() failing

I just want to find out whether there are any XML files BEFORE the FOR loop
 
Sorry, I'm not sure. I'm not much of a programmer, kind of a beginner myself to scripting. I would post the question in the Unix Scripting forum here --> forum822 Those guys can easily help you.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top