LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-04-2008, 04:30 AM   #1
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Rep: Reputation: 15
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..
 
Old 07-04-2008, 04:51 AM   #2
uppman
Member
 
Registered: Jul 2008
Location: Stockholm, Sweden
Distribution: Slackware 13.1 32-bit
Posts: 145

Rep: Reputation: 24
Change:
while [ "N" -lt "$count" ]

To:
while [ $N -lt $count ]

That should fix the loop.

---------
http://linuxconfig.dyndns.org:1184/lazy/LazyLinuxWiki
 
Old 07-04-2008, 04:54 AM   #3
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 39
You're missing a '$'.
Code:
while [ "$N" -lt "$count" ]
should work
 
Old 07-04-2008, 05:03 AM   #4
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
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..?
 
Old 07-04-2008, 05:09 AM   #5
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 39
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.
 
Old 07-04-2008, 05:13 AM   #6
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
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...
 
Old 07-04-2008, 05:22 AM   #7
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 39
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..
 
Old 07-04-2008, 05:25 AM   #8
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
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..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 07:24 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 07:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM


All times are GMT -5. The time now is 04:49 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration