Shweta_Singh
asked on
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 !
typeset -AE usage (where usage is the name of te array), but it shows me typeset: bad option(s)
Urgent reply needed.
Thanks !
ASKER
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 !
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 !
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].
http:Q_20771322.html#9580607
and http:Q_20894122.html
The Ksh array can have a MAX 512 elements. [0-511].
ASKER
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 !
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 !
This post looks like homework, please reread out member agreement:
https://www.experts-exchange.com/help.jsp#hi130
ASKER
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 !!
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 !!
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
########################## ########## ########## ########## ########## ########## ########## ########## ####getDis kUsage ()
{
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)
If I undestood your problem, check the following:
==========================
#!/bin/bash
##########################
##########################
# Return Users
##########################
getUsersInPath ()
{
ls -l "${1}" | sed 1d | awk '{print $3}' | sort | uniq
}
##########################
##########################
##########################
{
user="${1}"
checkPath="${2}"
ls -l "${checkPath}" | sed 1d | grep ${user} | grep -v grep | awk 'BEGIN{sum=0} {sum+=$5} END{print sum}'
}
##########################
##########################
##########################
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)
ASKER
Hey root_start:
Thanks for that response. If you could please explain me what parameters are to be passed in.
Thanks again
Shweta
Thanks for that response. If you could please explain me what parameters are to be passed in.
Thanks again
Shweta
Hi Shweta,
You only need to call the function "printDU" passing the "path".
e.g.
printDU "/etc"
Let me know if you have doubts.
You only need to call the function "printDU" passing the "path".
e.g.
printDU "/etc"
Let me know if you have doubts.
Ops... a correction to the script:
========================== ========== ========== ========== ========== ========== ========== ========== ====
#!/bin/bash
########################## ########## ########## ########## ########## ########## ########## ########## ####
########################## ########## ########## ########## ########## ########## ########## ########## ##### Return Users
########################## ########## ########## ########## ########## ########## ########## ########## ####getUse rsInPath ()
{
ls -l "${1}" | sed 1d | awk '{print $3}' | sort | uniq
}
########################## ########## ########## ########## ########## ########## ########## ########## ####
########################## ########## ########## ########## ########## ########## ########## ########## ##### Disk Usage by user
########################## ########## ########## ########## ########## ########## ########## ########## ####getDis kUsage ()
{
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.
==========================
#!/bin/bash
##########################
##########################
##########################
{
ls -l "${1}" | sed 1d | awk '{print $3}' | sort | uniq
}
##########################
##########################
##########################
{
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.
ASKER
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 .
The total disk usage .
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
find $1 -ls wouldn't work on std unix.
>>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
$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
ASKER
i knew that already .
I meant find (pathname) -ls wpn't work on std unix
Thanks
I meant find (pathname) -ls wpn't work on std unix
Thanks
>>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.e
979443 716 -rw-r--r-- 1 oraweb dba 729088 Jul 19 2005 /oracle/local/tmp/cjss_rep
$
ASKER
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
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
ASKER
$ find /appl -ls
find: bad option -ls
find: bad option -ls
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)
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)
ASKER
pls have a look here too
https://www.experts-exchange.com/questions/21894394/conditionals-in-ksh-scripting.html
https://www.experts-exchange.com/questions/21894394/conditionals-in-ksh-scripting.html
For KSH shell use:
set -A usage ...etc...(values separated by space)
For Linux use:
usage=( ...etc...values separated by space)