LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-02-2003, 11:06 AM   #1
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Rep: Reputation: 15
bash if statement question


i have written a script to run in cron to perform functions on files in a particular directory. Once complete the files are moved elsewhere.

My problem is that files will be placed in this directory periodically. I need my script to run nightly and start with an if statement to check to see if the directory is empty and if it is to exit, if not to continue on...

How can i do this?

thanks for any help...
 
Old 09-02-2003, 11:53 AM   #2
Steave
Member
 
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184

Rep: Reputation: 30
what about

Code:
if [ -z `ls /your/dir` ]
then
   don' do anything
else
  do something
fi
[ -z ... ]

checks if the string returned by ls /your/dir is empty -- meaning your directory is empty. notice the ` -- its important to use exactly this apostrophy!

HTH, Steave
 
Old 09-02-2003, 11:54 AM   #3
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Original Poster
Rep: Reputation: 15
figured it out...

#!/bin/bash
#
if [ `ls /test/dir/ | wc -l` -eq 0 ]
then
exit
else
echo "dir is not empty"
fi
 
Old 09-02-2003, 11:58 AM   #4
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Original Poster
Rep: Reputation: 15
thanks for the pointers Steave - i didn't know that one
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash assignment - using a grep inside an if statement lowpro2k3 Programming 4 03-25-2005 05:24 PM
bash statement os2 Programming 2 03-20-2005 10:13 PM
Bash select statement and spaces meshcurrent Linux - General 2 04-09-2003 09:35 AM
Need help adding an IF / ELSE statement to my Bash Script Relix Linux - General 1 08-01-2002 02:03 PM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 12:58 PM


All times are GMT -5. The time now is 11:12 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration