I wrote a simple array in KSH 88 and did the following:
# Vector methods
set -A ZeroFilesArr
GlobalIndex=-1
# Add element at the end
function ZeroFilesArr.push_back {
let GlobalIndex=$(($GlobalIndex + 1))
ZeroFilesArr[$GlobalIndex]=$1
return
}
# Return size
function ZeroFilesArr.size {
echo ${#ZeroFilesArr[@]}
}
# Clear content
function ZeroFilesArr.clear {
set -A ZeroFilesArr
let GlobalIndex=$((-1))
}
# Print contents in format
# Var1 \t Var2 \t ... \t Var_n
function ZeroFilesArr.print {
local tempString
for i in ${ZeroFilesArr[@]}
do
tempString="$tempString $i \t "
done
echo $tempString
}
I tested afterwards using the following:
ZeroFilesArr.push_back File1
ZeroFilesArr.push_back File2
ZeroFilesArr.push_back File3
echo $(ZeroFilesArr.size)
ZeroFilesArr.print
ZeroFilesArr.clear
echo $(ZeroFilesArr.size)
ZeroFilesArr.push_back File1
ZeroFilesArr.push_back File2
echo $(ZeroFilesArr.size)
ZeroFilesArr.print
ZeroFilesArr.clear
The output is the following
# kernel_coverage.sh
3
File1 File2 File3
0
2
File1 File2
But afterwards I did the following:
# Kernel Objects
# KSH Array <<FILES>> Contains strings that represent
# the objects that are published in the Kernel
set -A FILES $(ls /dev/daKernel/)
function CEILING {
local DIVIDEND=$1
local DIVISOR=$2
let RESULT=$((((${DIVIDEND} + ${DIVISOR}) - 1)/${DIVISOR}))
echo ${RESULT}
}
# Checks if the Parameter passed is
# a published object of the Kernel and returns
# the file size
# Returns a string with the size
function ZERO_FILE_CHECK {
local kernel_path="/dev/daKernel"
local FILE_NAME="$kernel_path/$1"
local retval=$(ls -l ${FILE_NAME} | awk {'print $5'})
echo $retval
}
# Checks and returns a statistic of how
# many files are in zero in the filesystem
# I.E. 3 out of 100 would return 3
function ZERO_FIlES_CHECK {
local let FILES_SIZE=${#FILES[@]}
local COUNTER=0;
for f in "${FILES[@]}"
do
local let n=$(ZERO_FILE_CHECK $f)
if [ $n -eq 0 ]; then
let COUNTER=$(($COUNTER+1))
fi
done
local PERCENTAGE=0
let PERCENTAGE=$(($COUNTER * 100))
let PERCENTAGE=$(CEILING $PERCENTAGE $FILES_SIZE)
echo $PERCENTAGE
}
# Check if the files are not in zero
# Input: An expected percentage, if bigger than
# this then there are too many files in zero size
# Depends on: ZERO_FIlES_CHECK and the FILES array
# Returns: 0 (Success) or 1 (Failure)
function CHECK_FILES {
local let default_percentage=$1
local let ZEROFILE_PERCENTAGE=$(ZERO_FIlES_CHECK)
if [[ $ZEROFILE_PERCENTAGE -ge $default_percentage ]]; then
return 0;
else
return 1;
fi
}
And it worked too, it returns me whether a folder has too much files in zero or not. So I though it was all set and on the road to happiness riding a wild broncunicorn.
But I was wrong.
I placed my magical push_back inside the ZERO_FIlES_CHECK function like the following:
function ZERO_FIlES_CHECK {
local let FILES_SIZE=${#FILES[@]}
local COUNTER=0;
for f in "${FILES[@]}"
do
local let n=$(ZERO_FILE_CHECK $f)
if [ $n -eq 0 ]; then
let COUNTER=$(($COUNTER+1))
ZeroFilesArr.push_back $f
fi
done
local PERCENTAGE=0
let PERCENTAGE=$(($COUNTER * 100))
let PERCENTAGE=$(CEILING $PERCENTAGE $FILES_SIZE)
echo $PERCENTAGE
}
And tested it using forcing it to find even the slightest file in zero
if CHECK_FILES 0; then
ZeroFilesArr.print
echo $(ZeroFilesArr.size)
fi
ZeroFilesArr.clear
if CHECK_FILES 0; then
ZeroFilesArr.print
echo $(ZeroFilesArr.size)
fi
But it always prints the following:
# kernel_coverage.sh
** An empty space here indicating NULL**
0
** An empty space here indicating NULL**
0
After trying several ways I am burned out, it seems I can't come up with a clear solution
Any help would be greatly appreciated
I'm thinking that I have a problem with variable scoping, but I though the global were globals just like anywhere else.