LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   nesting a statement in a loop (https://www.linuxquestions.org/questions/linux-newbie-8/nesting-a-statement-in-a-loop-4175482193/)

casperdaghost 10-25-2013 03:50 PM

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 :

Code:

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


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.

Robhogg 10-26-2013 10:09 AM

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:

Code:

populated=$(ls /production/archive/production/web/docs/$i/docs/etc/etc_html/ | wc -w)
if [ "$populated" -eq "0" ]; then
  echo "${i}: Population zero"
fi

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.

schneidz 10-27-2013 03:08 PM

Don't parse the output of ls. Just use the find command.
Code:

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);
done


Firerat 10-27-2013 03:20 PM

http://mywiki.wooledge.org/ParsingLs

more info on what schneidz is talking about


All times are GMT -5. The time now is 06:38 PM.