LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-25-2013, 03:50 PM   #1
casperdaghost
Member
 
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 :

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.
 
Old 10-26-2013, 10:09 AM   #2
Robhogg
Member
 
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:

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.
 
Old 10-27-2013, 03:08 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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

Last edited by schneidz; 10-27-2013 at 03:15 PM.
 
Old 10-27-2013, 03:20 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
http://mywiki.wooledge.org/ParsingLs

more info on what schneidz is talking about
 
  


Reply



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 08:43 AM
Loop Expect statement dnoy Linux - Newbie 1 09-13-2012 07:56 PM
if statement in a for loop dsmith8890 Programming 6 05-31-2012 01:29 AM
A for loop in an if statement tcsh leavage Programming 2 08-17-2011 11:32 PM
Need help with for loop + if statement Thaidog Programming 5 05-06-2011 09:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:20 PM.

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
Open Source Consulting | Domain Registration