URGENT !!! Associative arrays in ksh scripting

How do I create and initialize an associative array. I tried :

typeset -AE usage (where usage is the name of te array), but it shows me    typeset: bad option(s)  

Urgent reply needed.
Thanks !
Shweta_SinghAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
MikeOM_DBAConnect With a Mentor Commented:

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

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

PS: Parameter 1 ($1) is the top direcctory name from which you want to sum the disk usage.


0
 
MikeOM_DBACommented:


For KSH shell use:

set -A usage ...etc...(values separated by space)

For Linux use:

usage=( ...etc...values separated by space)

0
 
Shweta_SinghAuthor Commented:
How do I create and initialize an associative array. I tried :

typeset -AE usage (where usage is the name of te array), but it shows me    typeset: bad option(s)  

Also, I can assign values only one by one, through a loop and not seperated by spaces

Urgent reply needed.
Thanks !
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
yuzhCommented:
Please have a look at the example in:

http:Q_20771322.html#9580607

and http:Q_20894122.html

The Ksh array can have a MAX 512 elements. [0-511].
0
 
Shweta_SinghAuthor Commented:
I want to write a ksh (korn shell) script to show disk usage per user.

Description:  
The script should take in a single command-line argument--the directory name, and display the usage in MB per user.  Please comment your code for readability.  I tried to use the Unix ‘du’ command or you can write your own method to accomplish this task.


Thnaks !
0
 
MikeOM_DBACommented:

This post looks like homework, please reread out member agreement:
http://www.experts-exchange.com/help.jsp#hi130


0
 
Shweta_SinghAuthor Commented:
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
 
root_startCommented:
Hi Shweta_Singh,

If I undestood your problem, check the following:
====================================================================================================
#!/bin/bash
####################################################################################################

####################################################################################################
# Return Users
####################################################################################################
getUsersInPath ()
{
      ls -l "${1}" | sed 1d | awk '{print $3}' | sort | uniq
}
####################################################################################################

##################################################################################################### Disk Usage by user
####################################################################################################getDiskUsage ()
{
      user="${1}"
      checkPath="${2}"
      ls -l "${checkPath}" | sed 1d | grep ${user} | grep -v grep | awk 'BEGIN{sum=0} {sum+=$5} END{print sum}'
}
####################################################################################################

##################################################################################################### Print Disk Usage
####################################################################################################
printDU ()
{
      checkPath="${1}"
      for user in `getUsersInPath "${checkPath}"`
      do
            echo "User: [${user}] - Disc usage: [`getDiskUsage ${user} ${checkPath}`] - Path: [${checkPath}]."
      done      
}
####################################################################################################

printDU "."

====================================================================================================

It is really simple, but I think it does what you were expecting.

If you have questions, post here.

I hope it helps. =0)
0
 
Shweta_SinghAuthor Commented:
Hey root_start:

Thanks for that response. If you could please explain me what parameters are to be passed in.

Thanks again
Shweta
0
 
root_startCommented:
Hi Shweta,

You only need to call the function "printDU" passing the "path".
e.g.

printDU "/etc"

Let me know if you have doubts.
0
 
root_startCommented:
Ops... a correction to the script:
====================================================================================================
#!/bin/bash
####################################################################################################

##################################################################################################### Return Users
####################################################################################################getUsersInPath ()
{
        ls -l "${1}" | sed 1d | awk '{print $3}' | sort | uniq
}
####################################################################################################

##################################################################################################### Disk Usage by user
####################################################################################################getDiskUsage ()
{
        user="${1}"
        checkPath="${2}"
        ls -l "${checkPath}" | sed 1d | grep ${user} | grep -v grep | awk 'BEGIN{sum=0} {sum+=$5} END{print sum}'
}
####################################################################################################

####################################################################################################
# Print Disk Usage
####################################################################################################
printDU ()
{
        checkPath="${1}"
        for user in `getUsersInPath "${checkPath}"`
        do
                echo "User: [${user}] - Disc usage: [`getDiskUsage ${user} "${checkPath}"`] - Path: [${checkPath}]."
        done
}
####################################################################################################

printDU "."
====================================================================================================

I add a double quotes to when I call, to the variable ${checkPath}.
getDiskUsage ${user} "${checkPath}"

Otherwise if the script faces a path with a space on it, the script would fail.
0
 
Shweta_SinghAuthor Commented:
The disk usage we would get by this method would be incorrect . The disk usage that this shows is ll | awk '{print $5}'  where as what I want is du -sk (path)

The total disk usage .

0
 
Shweta_SinghAuthor Commented:
find $1 -ls wouldn't work on std unix.
0
 
yuzhCommented:
>>find $1 -ls wouldn't work on std unix.

$1 is the first commandline arg, in commandling you type in:

/path-to/scipt /home

/home is $1

Please have a look at the following page to learn some basic scripting:

http://steve-parker.org/sh/sh.shtml
http://www.shelldorado.com/links/#tutorials
http://www.codebox.8m.com/shellscripting.htm

0
 
Shweta_SinghAuthor Commented:
i knew that already .

I meant find (pathname) -ls wpn't work on std unix

Thanks
0
 
MikeOM_DBACommented:

>>I meant find (pathname) -ls wpn't work on std unix

Yes it will:

$ find /oracle/local/tmp -ls
979441    4 drwxrwxr-x   2 oraweb   dba          4096 Jul 19  2005 /oracle/local/tmp
979442 1528 -rw-r--r--   1 oraweb   dba       1556681 Jul 19  2005 /oracle/local/tmp/Agenda.ear
979443  716 -rw-r--r--   1 oraweb   dba        729088 Jul 19  2005 /oracle/local/tmp/cjss_report77.rdf
$
0
 
Shweta_SinghAuthor Commented:
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
 
Shweta_SinghAuthor Commented:
$ find /appl -ls
find: bad option -ls
0
 
root_startCommented:
Hi Shweta_Singh,

I changed one line in the script I posted above to use the "du -sk" command.
Check below:
===================================================================================================
ls -l "${checkPath}" | sed 1d | grep "${user}" | grep -v grep | awk '{nfields=NF; path="'${checkPath}'"; print path"/"$nfields}' | xargs du -sk 2>> /dev/null | awk 'BEGIN{sum=0} {sum+=$1} END{print sum}'
===================================================================================================

Just change the following line in the script:
     ls -l "${checkPath}" | sed 1d | grep ${user} | grep -v grep | awk 'BEGIN{sum=0} {sum+=$5} END{print sum}'
by the one I posted above.

I hope it helps. =0)
0
 
Shweta_SinghAuthor Commented:
0
All Courses

From novice to tech pro — start learning today.