I am trying to make a hash table with the file name of all files in a directory and another integer number. Like "File Name" : "number". The code should be in bash 4.x. This is the code which I wrote:
#!/bin/bash
DIR=`ls`
declare -A ARRAY
ZERO=0
for FILES in $DIR
do
echo "We have $FILES"
ARRAY+=(["$FILES"]="$ZERO")
done
echo "Done with filling up array!"
for file in "${ARRAY[@]}" ; do
KEY="${file%%:*}"
VALUE="${file##*:}"
printf "%s has number %s.\n" "$KEY" "$VALUE"
done
echo "We are done here!"
echo "Check: "
printf "%s has the number %s\n" "${ARRAY[1]%%:*}" "${ARRAY[1]##*:}"
and it gives this output:
[Anil@computer68 test]$ bash kenzo.sh
bash kenzo.sh
We have dafuq.sh
We have hello.cpp
We have kenzo.sh
Done with filling up array!
0 has number 0.
0 has number 0.
0 has number 0.
We are done here!
Check:
has the number
How do I fix it? None of the elements in the hash table have any values of the file name and the number.