Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 10-25-2013, 04:50 PM   #1
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
nesting a statement in a loop

I have a list of values and want to loop an ls -ltr over them to see that they are populated.
If they are not populated, I want to know that too.

I want to make sure that there is something there.
So i echo out the value

it goes like this :

 for i in broker1 broker2 energy external1 trader1 trader2;
do echo $i $(ls -ltr  /production/archive/production/web/docs/$i/docs/etc/etc_html/);

broker1 total 0 -rw-r--r-- 1 casper casper 9101 Oct 8 13:15 ETChtml.ASIA
broker2 total 12 -rw-r--r-- 1 casper casper 9205 Oct 8 13:16 ETChtml.ASIA
energy total 0
external1 total 12 -rw-r--r-- 1 casper casper 8315 Jul 3 12:01 ETChtml.ASIA
trader1 total 12 -rw-r--r-- 1 caspter casper 8706 Oct 8 13:16 ETChtml.ASIA
trader2 total 0
Do you see how I echo the $i and then put the ls into parenthesis so it prints out after the i?

So that way I can see which broker statements directories are not populated

i have been doing the same script for years. Is there a different way of accomplishing this - a different way of nesting the search in the loop?
My scripting needs to evolve.
Old 10-26-2013, 11:09 AM   #2
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Hi casper,

Is there something that this script isn't doing that would improve your day? For example, you could extend it to just report the directories that don't contain anything:

populated=$(ls /production/archive/production/web/docs/$i/docs/etc/etc_html/ | wc -w)
if [ "$populated" -eq "0" ]; then
  echo "${i}: Population zero"
Or, how about going further? If the server has access to a mail relay, you could get it to mail you when it finds an unpopulated directory. Run it from cron, and you could then avoid the need to run it manually every day.

On the other hand, if this script is already meeting your needs, why not think through other tedious regular tasks, and find one of those that could be scripted.

Of course, there are always different ways of doing something. Sometimes, though, the other ways are more... stupid than what you already have. If it ain't broke, don't fix it.
Old 10-27-2013, 04:08 PM   #3
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,149

Rep: Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887
Don't parse the output of ls. Just use the find command.
 for i in broker1 broker2 energy external1 trader1 trader2;
do echo $i: $(find /production/archive/production/web/docs/$i/docs/etc/etc_html/ -type f);

Last edited by schneidz; 10-27-2013 at 04:15 PM.
Old 10-27-2013, 04:20 PM   #4
Senior Member
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444

more info on what schneidz is talking about


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
[SOLVED] Using while loop & select statement - Loop issues Kustom42 Programming 4 05-17-2013 09:43 AM
Loop Expect statement dnoy Linux - Newbie 1 09-13-2012 08:56 PM
if statement in a for loop dsmith8890 Programming 6 05-31-2012 02:29 AM
A for loop in an if statement tcsh leavage Programming 2 08-18-2011 12:32 AM
Need help with for loop + if statement Thaidog Programming 5 05-06-2011 10:54 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:40 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration