Solved

I want to write a ksh (korn shell) script to show disk usage per user.

Posted on 2006-06-13
9
835 Views
Last Modified: 2013-12-26
I want to write a ksh (korn shell) script to show disk usage per user. The script should take in a single command-line argument--the directory name, and display the usage in MB per user.

I have managed to get the files listings in the directory and then check who the owner of the file was. But, I am unable to associate the space taken up by the file to it's particular owner. If I could find a way to do that, I would simply add the space of the files of similar owners. Please let me know, if I'm thinking wrong and please suggest on how to proceed further. Associateive arrays might help but I dont know that they exist in ksh or not. If yes, then how do I create one and set values to it ??

NOTE: Associative arrays are different than the normal arrays. Please dont give me a way to declare a simple array. I tried typeset to create an associative array, but it dosen't work !!
0
Comment
Question by:Shweta_Singh
[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
  • 5
  • 4
9 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 16894941

You may need to use 'awk' to do it.
0
 

Author Comment

by:Shweta_Singh
ID: 16894977
Could you please write a sample code to show me what you mean ?
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 16895788
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 16895827

PS: What have you got coded until now?
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 16895938
0
 

Author Comment

by:Shweta_Singh
ID: 16896842
okay, my code as for now looks like this :


#!/usr/bin/ksh

#CREATE AN ARRAY AND STORE O/P OF du IN IT
set -A users `du -ak $1`

i=0
while [ $i -lt ${#users[*]} ]  # GO THRU THE ARRAY
do
    if [ [`test -f $users[$i]`] ]; then  #CHECK IF IT'S A FILE.

print ${users[$i]}

        #-- Get the values
        #diskspace=$users[$i]
        #entry_path=$users[$i+1]


        # GET THE OWNER NAME.
        owner=[`ls -la $entry_path | awk {'print $3'}`]

        #STUFF IT IN THE ASSOCIATIVE ARRAY
        typeset -A usage  # CREATING THE ARRAY
        #  usage["$owner"]=$diskspace   #OWNER REFERS TO THE SPACE HE TAKES UP.
        usage[ $owner ] = usage[ $owner ] + $diskspace  #OWNER REFERS TO THE SPACE HE TAKES UP.

#        print ${users[$i]}
    fi
    i=$i+2
done

0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 total points
ID: 16897095

Instead of using 'du' try something more simple like:

#!/bin/ksh
find $1 -ls|\
awk '{o[$5]=o[$5]+$7;}
END {for (i in o) print i " uses " o[i] " bytes.";}'
0
 

Author Comment

by:Shweta_Singh
ID: 16943246
Okay,
This is where I have reached uptil yet .

All you need to know is that I made an array and that I need to access it's variables which i'm unable to .
Pls help.


#!/usr/bin/ksh
#CREATE AN ARRAY TO STORE OWNERS TO COMPARE
set -A owners `ls -lR "${1}" 2>/dev/null | sed 1d | awk '{print $3}' | sort | uniq`

#LOOP OVER THE ARRAY TO GET THE OWNERS ONE BY ONE TO COMPARE
i=0
while [ $i -lt ${#owners[*]} ]
do
        `ls -lsR "${1}" | awk '/$owners[$i]/ {disk_usage += $6} END {print $owners[$i], " : ", 1*disk_usage, " MB"}'`

        ((i=i+1))
done
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

705 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