-   Linux - Newbie (
-   -   Help With a Shell Script (

djfog 07-04-2008 04:30 AM

Help With a Shell Script
Hi to all Linux Users...I have this script:

read -p "give directory's name: " NAME
cd $NAME
count=$(ls -l | grep '\d\r\w' | wc -l)
while [ "N" -lt "$count" ]
  echo "give the name of a sub-directory"
  read NAME1
  cd $NAME1
  echo "the number of files in this sub-directory isi:"
  ls -1 | grep '\-\r\w' | wc -l
  cd ..

But I can't get the while loop to work.I want it so that I can find how many files has each sub-directory.Thanks in advance..

uppman 07-04-2008 04:51 AM

while [ "N" -lt "$count" ]

while [ $N -lt $count ]

That should fix the loop.


indeliblestamp 07-04-2008 04:54 AM

You're missing a '$'.

while [ "$N" -lt "$count" ]
should work

djfog 07-04-2008 05:03 AM

Well,thanks.The loop is now fixed but I'm having another problem.I always get that the number of files is 0.Any ideas..?

indeliblestamp 07-04-2008 05:09 AM

yes, you used ls -1 (one) instead of ls -l (lower case L).
Actually if your intention is to get a directory from the user, and print the list of files in each of its subdirectories, will this code help?

read -p "give directory's name: " NAME
DIRLIST=`find $NAME -type d -maxdepth 1`
echo "dirlist is $DIRLIST"
for dir in $DIRLIST
if [ "$NAME" != "$dir" ]
echo "Here are the files in $dir"
ls -1 $dir

The only difference is that the user doesn't have to prompt each subdirectory's name in this case.

djfog 07-04-2008 05:13 AM

This code is great.Is even better from what I was thinking,but I'm getting a warning with maxdepth.It doesn't matter though.Thanks again...

indeliblestamp 07-04-2008 05:22 AM

Glad it helped. I didn't get any warnings with the folder I tested, so you might want to google the warning text and see..

djfog 07-04-2008 05:25 AM

It was a quite silly warning.It just needed to write this command:

DIRLIST=`find $NAME -type d -maxdepth 1`
like this:

DIRLIST=`find $NAME -maxdepth 1 -type d`
Thanks again for your help..

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