Skip to content

Commit fbeaa91

Browse files
committed
added counting of each extension that is created
1 parent 3235b99 commit fbeaa91

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

randtree.sh

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,19 @@ MINSIZE=2000
4343
# the containing folder for the random tree
4444
BASENAME=randtree
4545
# extensions for the random files
46-
EXTS=(md log json js php jpg png html css)
46+
EXTS=(md log json js php jpg png html css txt)
47+
###########################################################
48+
# optional, will increment a count for each EXTS used and
49+
# show it at the end
50+
INVEN=true
51+
# this will use the EXTS array to build an array of counters
52+
if [ "$INVEN" = true ]; then
53+
declare -A ext_inven
54+
for ext in "${EXTS[@]}"
55+
do
56+
ext_inven+=(["$ext"]=0)
57+
done
58+
fi
4759
###########################################################
4860
# "mutable" echoing
4961
SILENT=true
@@ -68,13 +80,20 @@ mkranfiles() {
6880
num_EXTS=${#EXTS[*]}
6981
for i in $(seq 1 $NUMRFILES)
7082
do
71-
# Random file name
72-
fname=`mktemp -u XXXXXXX."${EXTS[$((RANDOM%num_EXTS))]}"`
83+
# Random file extension
84+
fext="${EXTS[$((RANDOM%num_EXTS))]}"
85+
# Random file name and extension
86+
fname=`mktemp -u XXXXXXX."$fext"`
7387
# Random file size within a specific range
7488
fsize=$(($MINSIZE + $RANDOM % $MAXSIZE))
7589
# Add random content to files using allowed chars
7690
cat /dev/urandom |tr -dc A-Z-a-z-0-9-" " | head -c${1:-$fsize} > $fname
7791
mutecho $fname
92+
if [ "$INVEN" = true ]; then
93+
tmp=${ext_inven[$fext]}
94+
tmp=$((tmp+=1))
95+
ext_inven+=([$fext]=$tmp)
96+
fi
7897
done
7998
}
8099
# make a filled random folder tree
@@ -111,3 +130,9 @@ do
111130
done
112131
echo "Finshed in $PWD"
113132

133+
# show how many of each file extension we created
134+
if [ "$INVEN" = true ]; then
135+
echo
136+
echo "File Extension Counts:"
137+
for x in "${!ext_inven[@]}"; do printf "[%s] = %s\n" "$x" "${ext_inven[$x]}" ; done
138+
fi

0 commit comments

Comments
 (0)