Solved

loop in shell script

Posted on 2013-06-26
16
499 Views
Last Modified: 2013-06-27
Hello  Expert,

I want to configure loop in shell script...
#!/bin/bash
du -h --max-depth=1 /root/Desktop/inux/test1 | sort -n -r > /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/test2 | sort -n -r >> /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/test3 | sort -n -r >> /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/test4 | sort -n -r >> /root/Desktop/userlist.csv

I've four folder in under linux..Now I'm trying to do run du command in one line..which take all folder disk usage size under linux directory....

if anybody know  about it....please reply on the SAME...
0
Comment
Question by:xeroxzerox
  • 7
  • 5
  • 3
  • +1
16 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
Comment Utility
du -h --max-depth=1 /root/Desktop/inux/test[1-4] | sort -n -r > /root/Desktop/userlist.csv
0
 
LVL 6

Author Comment

by:xeroxzerox
Comment Utility
Thanks for reply ozo,
But if i've like this what can i do.... (user name will be change)
du -h --max-depth=1 /root/Desktop/inux/xerox1 | sort -n -r > /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/zerox | sort -n -r >> /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/neil | sort -n -r >> /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/prey.ody | sort -n -r >> /root/Desktop/userlist.csv
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
du -h --max-depth=1 /root/Desktop/inux/{xerox1,zerox,neil,prey.ody} | sort -n -r > /root/Desktop/userlist.csv
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 100 total points
Comment Utility
Or if you do want a for loop

for i in xerox1 zerox neil prey.ody
do
   du -h --max-depth=1 /root/Desktop/$i | sort -nr >>/root/Desktop/userlist.csv
done

Open in new window

0
 
LVL 6

Author Comment

by:xeroxzerox
Comment Utility
OZO  your command output look like this
8.0K    /root/Desktop/inux/prey.ody
8.0K    /root/Desktop/inux/xerox/
8.0K    /root/Desktop/inux/zerox/
4.0K    /root/Desktop/inux/prey.ody/user1
4.0K    /root/Desktop/inux/xerox/user2
4.0K    /root/Desktop/inux/zerox/user3

But when I run that command
du -h --max-depth=1 /root/Desktop/inux/xerox1 | sort -n -r > /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/zerox | sort -n -r >> /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/neil | sort -n -r >> /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/prey.ody | sort -n -r >> /root/Desktop/userlist.csv

Output is like this:
8.0K    /root/Desktop/inux/prey.ody
4.0K    /root/Desktop/inux/prey.ody/user1
8.0K    /root/Desktop/inux/xerox/
4.0K    /root/Desktop/inux/xerox/user2
8.0K    /root/Desktop/inux/zerox/
4.0K    /root/Desktop/inux/zerox/user3

can we get output look like this by your command....
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
You can modify the sort options, but it's probably easier to run Tintin's version
0
 
LVL 6

Author Comment

by:xeroxzerox
Comment Utility
I've ran Tintin loop but it show  only one Directory output..like.this...

8.0K    /root/Desktop/inux/prey.ody
4.0K    /root/Desktop/inux/prey.ody/user1
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Did you really run Tintin's loop?  or did you change the >> in  Tintin's loop to >
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 6

Author Comment

by:xeroxzerox
Comment Utility
#!/bin/bash
for i in xerox1 zerox neil prey.ody
do
   du -h --max-depth=1 /root/Desktop/$i | sort -nr >> /root/Desktop/userlist.csv
done
mutt -s "Test email with attachments and content" -a /root/Desktop/userlist.csv  -- xerox.zerox@texas.edu < /root/Desktop/signature.txt

when i run it 2 times...it add output data userlist.csv in 2 times.
when again i've run it then output data in 3 times...It's not over-right data in userlist.csv file...Because we are using >> here. if i use > then it show only last directory details not all...
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
do the > before the loop
0
 
LVL 6

Author Comment

by:xeroxzerox
Comment Utility
i'm not understand where i shall be put > in it.

i think according to you it look like

du -h --max-depth=1 /root/Desktop/$i | sort -nr > /root/Desktop/userlist.csv
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 100 total points
Comment Utility
You need to do the ">" before the loop (to empty out the output file), and ">>" within the loop so that results are appended to the file and don't overwrite it.  You end up with:
#!/bin/bash
> /root/Desktop/userlist.csv
for i in xerox1 zerox neil prey.ody
do
   du -h --max-depth=1 /root/Desktop/$i | sort -nr >> /root/Desktop/userlist.csv
done
mutt -s "Test email with attachments and content" -a /root/Desktop/userlist.csv  -- xerox.zerox@texas.edu < /root/Desktop/signature.txt

Open in new window

0
 
LVL 6

Author Comment

by:xeroxzerox
Comment Utility
Hi,

I've ran simon 3270 command Now output like this:

4.0K    /root/Desktop/inux/prey.ody/user1
8.0K    /root/Desktop/inux/prey.ody
8.0K    /root/Desktop/inux/xerox/
4.0K    /root/Desktop/inux/xerox/user2
8.0K    /root/Desktop/inux/zerox/
4.0K    /root/Desktop/inux/zerox/user3

Now prey.ody directory in under user1....why it's not showing output like this..

8.0K    /root/Desktop/inux/prey.ody
4.0K    /root/Desktop/inux/prey.ody/user1
8.0K    /root/Desktop/inux/xerox/
4.0K    /root/Desktop/inux/xerox/user2
8.0K    /root/Desktop/inux/zerox/
4.0K    /root/Desktop/inux/zerox/user3

So...issue in sort or another one in script...
0
 
LVL 6

Author Closing Comment

by:xeroxzerox
Comment Utility
thanks all of you
0
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
Not sure why your entries appear in the "wrong" order, but I think you'll have to drop the "-h" parameter on du.  If, say, the prey.ody directory has 1.1G, while prey.ody/user1 has 900M, the "sort -nr" will say that prey.ody/user1 is bigger - it doesn't understand the M or G suffixes.  For example, on my machine:
sar@localhost:~$ du -h --max-depth=1 /home/sar/Desktop | sort -nr
23M	/home/sar/Desktop
4.0K	/home/sar/Desktop/2011_Oct
1.7M	/home/sar/Desktop/iTunes Library.itlp
sar@localhost:~$ du --max-depth=1 /home/sar/Desktop | sort -nr
22884	/home/sar/Desktop
1724	/home/sar/Desktop/iTunes Library.itlp
4	/home/sar/Desktop/2011_Oct
sar@localhost:~$ 

Open in new window

So the 4.0K in the 2011_Oct directory is shown as being bigger than the 1.7M of iTunes library with "-h", but without, they are in the right order.
0
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
Alternatively, use the "-h" (sort in "human numeric") parameter of sort rather than "-n", so that it *does* respond to K/M/G suffixes.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now