Hi,
Attached is a WIP script I've just written which tries to spot typos in
a file, based on the filename itself.
Yesterday, I've posted some finding done with this script.
Today, I share if someone finds it useful, want to improve it or just
want to take the idea.
As, I'm not a bash guru, it is neither optimal, nor well written.
But it seems to work as I expect.
The name of a file can be a good source of information to spot typo in
the code itself. This can help spot typo in comment or string, but also
# wrongly named functions or constant.
3 checks are implemented. They can be disabled individually.
The filename should sometime be tweaked a bit to only take the part
before or after a '-' or a '_'. (some regex pattern are in the script
for that, just comment/un-comment)
The 2 last checks generate lot of false positives.
It can find some few things, but honestly, the semantic should be improved.
Just in case s.o. find it useful and want to use it to clean-up a few
things.
Best regards
CJ
#!/bin/bash
#
# The name of a file can be a good source of information to spot typo in
# the code itself. This can help spot typo in comment or string, but also
# wrongly named functions or constant
#
# This script implements a few 'grep' with variations of the filename
# (inversion of 2 chars, missing char, wrong char)
#
# Enable/disable each test
do_inversion=1;
do_missing=0;
do_wrong=0;
for f in $(find . -name '*.c' -type f); do
# echo $f;
pattern=$(basename $f .c)
#echo "- " $pattern;
# This can be used to extract sub-string in the filename
# part=$(echo $pattern | perl -lpe 's/(.*)-.*/$1/')
# part=$(echo $pattern | perl -lpe 's/.*-(.*)/$1/')
part=$(echo $pattern)
# echo "-->" $part " (" ${#part} ")" ;
############################
# Check for char inversion #
############################
for (( n=0; n<${#part}-1; n++ )); do
x="${part:$n:1}";
y="${part:$n+1:1}";
first="${part:1:1}";
# echo $x " - " $y;
if (($do_inversion==1)) && (( ${#part}>5 )) && [ "$x" != "$y" ] && [ "$first" != "-" ]; then
perm="";
for (( i=0; i<n; i++ )); do
perm="$perm${part:$i:1}";
done
perm="$perm${part:$n+1:1}";
perm="$perm${part:$n:1}";
for (( i=n+2; i<${#part}; i++ )); do
perm="$perm${part:$i:1}";
done
# echo -e $perm "\n";
# echo "grep $perm $f";
if grep -i "$perm" "$f"; then
echo -e "\n";
echo "grep -i $perm $f";
echo " Inversion -->" $part " (" ${#part} ")" ;
echo "############################";
echo -e "\n";
fi
fi
done
##########################
# Check for missing char #
##########################
for (( n=1; n<${#part}-1; n++ )); do
if (($do_missing==1)) && (( ${#part}>5 )) && [ "${part:n:1}" != "_" ] && [ "${part:n:1}" != "-" ] && [ "${part:n:1}" != "${part:0:1}" ] && [ "${part:n:1}" != "${part:${#part}-1:1}" ]; then
perm="${part:0:n}";
perm="$perm${part:n+1:${#part}}";
# echo -e $perm "\n";
# echo "grep $perm $f";
if grep -i "$perm" "$f"; then
echo -e "\n";
echo "grep -i $perm $f";
echo " Missing -->" $part " (" ${#part} ")" ;
echo "############################";
echo -e "\n";
fi
fi
done
########################
# Check for wrong char #
########################
for (( n=1; n<${#part}-1; n++ )); do
if (($do_wrong==1)) && (( ${#part}>5 )); then
perm="${part:0:n}";
perm="$perm[^ -_${part:n:1}]";
perm="$perm${part:n+1:${#part}}";
# echo -e $perm "\n";
# echo "grep $perm $f";
if grep -i "$perm" "$f"; then
echo -e "\n";
echo "grep -i $perm $f";
echo " Wrong -->" $part " (" ${#part} ")" ;
echo "############################";
echo -e "\n";
fi
fi
done
done