please check this code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting please check this code
# 1  
Old 11-17-2010
please check this code

hi,
i m checking the directory is empty or not

Code:
  FILE=""
  DIR="/ann/a1"

if [ "$(ls -A $dir)" ]; then
       echo "$dir is not Empty"
  else
      echo "$dir is Empty"
  fi

i m always getting this directory is not empty, even if the directory is empty



please point where i had went wrong??
# 2  
Old 11-17-2010
FILE=""
DIR="/ann/a1"

if [ -z "$(ls -A $DIR)" ]; then
echo "$dir is not Empty"
else
echo "$dir is Empty"
fi
# 3  
Old 11-17-2010
hi ,
now im getting only dir is empty.... for what ever condition.....



when ever i had cehcked this line ls -A $dir
its giving me the error like this directory not accessible Smilie


regards
Angel
# 4  
Old 11-17-2010
Couple of issues here.
1) Environment Variable names are case-sensitive. $DIR and $dir are not the same variable.
2) Your messages appear to be the wrong way round. When we find no files (and the -z condition is satisfied) our message should be "Empty".

Code:
FILE=""
dir="/ann/a1"

if [ -z "$(ls $dir)" ]; then
     echo "$dir is Empty"
else
     echo "$dir is not Empty"
fi

This User Gave Thanks to methyl For This Post:
# 5  
Old 11-17-2010
Quote:
Originally Posted by methyl
Code:
FILE=""
dir="/ann/a1"

if [ -z "$(ls $dir)" ]; then
else
     echo "$dir is not Empty"
fi

There's no need of process substitution : (just take care that the condition is reverted)
Code:
dir="/ann/a1"
if ls $dir; then
     echo "$dir is not Empty"
else
     echo "$dir is Empty"
fi

# 6  
Old 11-17-2010
Quote:
Originally Posted by anurag.singh
FILE=""
DIR="/ann/a1"

if [ -z "$(ls -A $DIR)" ]; then
echo "$dir is not Empty"
else
echo "$dir is Empty"
fi
This is wrong : -z is true if empty
so... it should be

Code:
if [ -z "$(ls -A $DIR)" ]; then
echo "$dir IS Empty"
else
echo "$dir is NOT Empty"
fi

or

Code:
if [ -n "$(ls -A $DIR)" ]; then
echo "$dir is not Empty"
else
echo "$dir is Empty"
fi

# 7  
Old 11-17-2010
Code:
emptydir() { set -- "$1"/*; ! [ -e "$1" ] ;}
if emptydir "$DIR"; then
  echo "dir is Empty"
else
  echo "dir is not Empty"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with code to check if file systems are mounted

Hi I need to have a piece of code that check if all file systems are mounted or not. I have to pieces of information like the output of the bdfcommand, and the file /etc/fstab. The first is: bdf Filesystem kbytes used avail %used Mounted on /dev/vg00/lvol3 2097152 266656... (3 Replies)
Discussion started by: fretagi
3 Replies

2. Shell Programming and Scripting

Check wget return code

hello check this script it jump to else part, in n both cases, (if files exist or not) wget $MIRROR/kkk.zip && wget $MIRROR/jjj.zip RC="$?" if ] then echo -e "$RED Ooops, Download links are broken...! $RESET" else echo -e "$GREEN Everything is fine, Cheers ... $RESET" fi (4 Replies)
Discussion started by: nimafire
4 Replies

3. Shell Programming and Scripting

Perl code to check date and check files in particular dir

Hi Experts, I am checking how to get day in Perl. If it is “Monday” I need to process…below is the pseudo code. Can you please prove the code for below condition. if (today=="Monday" ) { while (current_time LESS THAN 9:01 AM) ... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

4. Shell Programming and Scripting

PERL code to check if file exists

Hi Guy’s, I have this simple PERL code which checks whether the file exists . At the moment I am getting the following error This is the code I am using #!/usr/bin/perl Open (F, "home/work/PerlWork/dataFile") or die "Could not open the file:$!\"; Also how can I read the content of... (3 Replies)
Discussion started by: INHF
3 Replies

5. Shell Programming and Scripting

Find: return code check

Hi, I've a find command like, find /abcd/efgh -name "xxxx" 2> /dev/null > file1 what would be the condition to check if this returns none(means if the file "xxxx" is not present) ???? I tried checking with $? and it returns 72 to me but even the file is present also it returns... (6 Replies)
Discussion started by: skcvasanth
6 Replies

6. Programming

c++ code to check whether a list is circular or not

hi all, i need c++ code to check whether a list is circular or not... please help (8 Replies)
Discussion started by: vidyaj
8 Replies

7. Shell Programming and Scripting

Mail::Sender - How to Check Result Code?

I have a code block which sends a mail using Mail::Sender. Everything works great now. I just want to know how to check whether the status of sending mail is success or failure. Based on which I will log the result in my log file. How can I do this? Any idea please? (2 Replies)
Discussion started by: dahlia84
2 Replies

8. Web Development

[PHP] Server Check and Failover Code

Here is some sample PHP code you can run if you have a PHP web application that uses code or images from an ad server, image server, or content deliver network, and you want to check if it is working and if not, failover to another one: <?php $current_server = "server.domain.com"; // set... (0 Replies)
Discussion started by: Neo
0 Replies

9. Shell Programming and Scripting

How can i check my code ..

#!/bin/sh #For Daily Routine echo 'Checking in Progress ...' echo > status.txt echo >> status.txt for i in IVR011 IVR012 IVR021 IVR022 do echo >> status.txt echo ' ****** '$i ' *******' >> status.txt echo >> status.txt rsh $i df -k >> status.txt echo >> status.txt rsh $i sar 1 5... (5 Replies)
Discussion started by: Dastard
5 Replies

10. UNIX for Dummies Questions & Answers

check my code?? running shell from c?

Hi mates, i am trying to run a shell command from my C program in this case let is say the "ls" command. It copiles it okay, and even creates the new fork too. But seems to nothing is happening i mean it is not showing the result of the "ls" command. I don't know wat i am doing wrong. Any... (1 Reply)
Discussion started by: abdul
1 Replies
Login or Register to Ask a Question