LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 06-30-2008, 05:04 PM   #1
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Rep: Reputation: 15
Shell Scripting help---Newbie


Hello to all.I'm looking for a script that compares the number of files in two directories that I'll give as an input and prints the result of this comparison.I know it may seem easy,but I'm a newbie and in need of your help.Thanks in advance.
 
Old 06-30-2008, 05:27 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Sounds like a homework, so I shall not give you a direct answer. Programs / things you might find useful:
  • The program ls
  • The program wc
  • How to compare numbers in shell scripts. Check out the "CONDITIONAL EXPRESSIONS" section of the bash manual page.
Tell us what you tried, and we will correct you and point you in the right direction.
 
Old 06-30-2008, 06:07 PM   #3
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
Here is the script I've written,but it has some errors.

#!/bin/bash
echo "give the names of the 2 direcories"
read NAME1
read NAME2
cd $NAME1
ls|wc -l>file1
cp file1 ~
cd ..
cd $NAME2
ls|wc -l>file2
cp file2 ~
cd ..
x=tail +1 file1
y=tail +1 file2
if ($x>$y); then
echo "first directory has more files"
else if ($x=$y); then
echo "direcories have the same number of files"
else
echo "second directory has more files"
fi
fi

My problem now is in the fact that this script works for the correct number only the first time,for the rest compares one more file for each directory.Furthermore,I can't store the first line of the files to a variable for the comparison...thanks for the advices matthewg42
 
Old 06-30-2008, 07:58 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You can put shell script and other source code in [code] tags and it will look a lot better - for example, indentation is preserved.

A few observations which should help:
  • bash's read command can prompt for you, so you don't need to do an echo first. For example:
    Code:
    read -p "enter a directory: " NAME1
  • there's no need to re-direct the output of wc into a file. You can read it directly into a variable like this:
    Code:
    count=$(ls |wc -l)
    This is true for any command which produces output on the standard output file descriptor (i.e. most of them), although you should probably not do this when the output of the command could be a very large amount of data
  • Try "[" and "]" instead of "(" and ")" in the "if" part. Note that you need spaces between the "[" and "]" and each operand / operator.
  • The integer equality operator is "-eq", not "=", although "=" should work, because it is string comparison.
 
Old 07-01-2008, 03:50 AM   #5
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks again for your answer matthewg42.Your advices are very helpful.Here is a second the script :

Quote:
#!/bin/bash
read -p "enter the first directory: " NAME1
read -p "enter the second directory: " NAME2
cd $NAME1
x=$(ls |wc -l)
cd ..
cd $NAME2
y=$(ls |wc -l)
cd ..
if [ "$x > $y" ] ; then
echo "first directory has more files"
else if [ "$x -eq $y" ] ; then
echo "direcories have the same number of files"
else
echo "second directory has more files"
fi
fi

My problem now is that for every combination of directories every time prints first directory has more files.Where are my mistakes..?
Thanks in advance...
 
Old 07-01-2008, 04:06 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
After doing "cd [some directory]", you can do "cd -" to return to the previous directory. Consider when NAME1 is something like "directory/subdirectory" - the "cd .." will not return to the original directory, but "cd -" will.

In fact, you don't really need to change directory at all... you can do:
Code:
ls dirname
ls will output the contents of "dirname" if it is a directory, so you can assign your variables simply like this:
Code:
x=$(ls "$NAME1" |wc -l)
y=$(ls "$NAME2" |wc -l)
The greater than operator is -gt (-lt is less than, -ge is greater than or equal to, and so you can probably guess what -le is). In shellscript the ">" character re-directs the output of the statement to the left of the character into the file to the right of it. You will probably have some files whose names are numbers after running your script

Use "elif" not, "else if". Why every language on the planet has to invent a new way to say "else if" I do not know, but heh.

Use [code] tags, not [quote] tags when posting code. It looks much better (fixed width font, preserved whitespace).

Last edited by matthewg42; 07-01-2008 at 04:09 AM.
 
Old 07-01-2008, 04:45 AM   #7
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
Smile

Now my script looks like 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
But the problem remains.I always get that the first directory has more files,even if it's not true.Thanks for all your tips..They help me with other scripts as well.
 
Old 07-01-2008, 05:34 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You should quote both sides of the operands in the if statement (or neither side), i.e.
Code:
if [ "$x" -gt "$y" ] ; then
 
Old 07-01-2008, 05:54 AM   #9
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks for all your help matthewg42.You 've been very helpful.The script now works like a charm and is exactly what I need.
Thanks and again thanks...

Last edited by djfog; 07-01-2008 at 07:27 AM.
 
  


Reply



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
Problem with Fedora Shell Scripting.. Newbie here.. confulity Programming 10 03-09-2008 10:21 AM
newbie scripting: Writeing a Shell script Qu hoodedmanwithsythe Programming 12 06-10-2007 07:54 AM
Shell scripting help -- newbie rnj Linux - Newbie 1 09-12-2005 12:08 AM
Shell Scripting problems (newbie) yawgmoth81 Programming 11 02-24-2003 02:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:58 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration