Solved

loop in shell script

Posted on 2013-06-26
16
510 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 3
  • +1
16 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
ID: 39277507
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
ID: 39277523
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
ID: 39277528
du -h --max-depth=1 /root/Desktop/inux/{xerox1,zerox,neil,prey.ody} | sort -n -r > /root/Desktop/userlist.csv
0
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 100 total points
ID: 39277534
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
ID: 39277571
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
ID: 39277591
You can modify the sort options, but it's probably easier to run Tintin's version
0
 
LVL 6

Author Comment

by:xeroxzerox
ID: 39277627
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
ID: 39277660
Did you really run Tintin's loop?  or did you change the >> in  Tintin's loop to >
0
 
LVL 6

Author Comment

by:xeroxzerox
ID: 39277686
#!/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
ID: 39277688
do the > before the loop
0
 
LVL 6

Author Comment

by:xeroxzerox
ID: 39277703
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
ID: 39278666
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
ID: 39280248
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
ID: 39280258
thanks all of you
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39280797
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
ID: 39281603
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Fine Tune your automatic Updates for Ubuntu / Debian
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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.
Suggested Courses

623 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