I have a bash function that crawls through the current directory of the bash script and find for files that have a certain file extensions. Everything is working fine until I have files that have bash-related special characters like '-' in the filename.
My question is, how do I handle the dashes in the filename? Thank you in advance!
Directory
./1a.log
./1b.log
./1c.log
./1d file.log
./1e file_working.log
./1f-notworking.log #error
logparser.sh
read_files() {
files=()
file_ext="${FILE##*.}"
if [ -f "$FILE" ] && [[ $file_ext == log ]]; then
msg "${RED}Parsing file: ${CYAN}$FILE"
files+=($FILE)
elif [ -d "$FILE" ]; then
msg "${RED}Parsing file: ${BLUE}$FILE"
for FILENAME in "$FILE"/*; do
dir_ext="${FILENAME##*.}"
if [ -f $FILENAME ] && [[ $dir_ext == log ]]; then
files+=($FILENAME)
fi
done
else
msg "${RED}Unable to process: ${CYAN}$FILE .Skipping"
fi
}
Tracestack
[: syntax error: `-' unexpected