Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > Script to compare directories

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 07-01-2008   #1 (permalink)
Just Joined!
 
Join Date: Jun 2008
Posts: 25
Script to compare directories

I was trying to make write a shell script that would compare the number of files in 2 given directories.I 've come up with this :
Code:
#!/bin/bash
read -p "enter the first directory: " NAME1
read -p "enter the second directory: " NAME2
x=$(ls "$NAME1" |wc -l)
y=$(ls "$NAME2" |wc -l)
if [ "$x -gt $y" ] ; then
echo "first directory has more files"
elif [ "$x -eq $y" ] ;  then
echo "direcories have the same number of files"
else
echo "second directory has more files"
fi
My problem here is that the result is always "first directory has more files" even if that's not true.Where is my mistake or what should I do..?Thanks in advance..
djfog is offline  


Reply With Quote
Old 07-01-2008   #2 (permalink)
/etc/init.d/moderator
 
bigtomrodney's Avatar
 
Join Date: Nov 2004
Location: Sunny South-East of Ireland
Posts: 6,031
I haven't got a Linux box near me to check, but it would seem the quotation within the if test is incorrect. You are testing if the string, well...exists. A negative return is not likely from that the way it's phrased now. Try doing it this way with each variable quoted :-
Code:
#!/bin/bash
read -p "enter the first directory: " NAME1
read -p "enter the second directory: " NAME2
x=$(ls "$NAME1" |wc -l)
y=$(ls "$NAME2" |wc -l)
if [ "$x" -gt "$y" ] ; then
echo "first directory has more files"
elif [ "$x" -eq "$y" ] ;  then
echo "direcories have the same number of files"
else
echo "second directory has more files"
fi
__________________
Registered Linux user #378740
New members read here / Forum Rules
#linuxforums on irc.freenode.net
bigtomrodney is offline   Reply With Quote
Old 07-01-2008   #3 (permalink)
Just Joined!
 
Join Date: Jun 2008
Posts: 25
thanks my friend...it works great.
djfog is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 03:58 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2