LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help With a Shell Script (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-a-shell-script-653515/)

djfog 07-04-2008 04:30 AM

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

#!/bin/bash
read -p "give directory's name: " NAME
cd $NAME
count=$(ls -l | grep '\d\r\w' | wc -l)
N=1
while [ "N" -lt "$count" ]
do
  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 ..
  N=$[N+1]
done

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

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

To:
while [ $N -lt $count ]

That should fix the loop.

---------
http://linuxconfig.dyndns.org:1184/lazy/LazyLinuxWiki

indeliblestamp 07-04-2008 04:54 AM

You're missing a '$'.
Code:

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?
Code:

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

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:
Code:

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

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


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