Posted by Matt Williams
The korn shell does not support hashes, but only arrays and those are not associative. So here is a solution for adding hashes to the shell:
- Create your data. In this case, we're pulling in ps data:
1
2
|
psData=`ps -e -o pid,user,pcpu,vsz,rss|egrep -v '^ *PID'|awk 'BEGIN{OFS="+"}{$1=$1;printf("%s:'%s'.".$1.$0)}'`
|
Here we are formatting our output to look like this:
1234:'1234+matt+10+123456+1234789',...
- In order to access our values, we need to strip off the information prior to our key, as well as after the value:
1
2
3
4
5
6
7
8
|
expr=1234
case $psData in
$expr)
x=${psData##(${expr}:}
out=${xx,*}
;;
esac
|
This code takes the value of the expr variable, then, if it is in psData it strips everything prior to the hash values (including the key) and then everything after, so that you are left with the value.
Enjoy!