Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

loop in shell script

Posted on 2013-06-26
16
Medium Priority
?
511 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 1200 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 400 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 20

Assisted Solution

by:simon3270
simon3270 earned 400 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 20

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 20

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

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

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

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). …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

660 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