Solved

loop in shell script

Posted on 2013-06-26
16
509 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RHEL6 + dockers - No route to host 7 103
parallel rsync issues with Ubuntu 1 50
How to really determine how many  files in hosting 2 32
Linux 6 32
Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

734 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