0

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
user1934428
  • 19,864
  • 7
  • 42
  • 87
stranger
  • 134
  • 1
  • 7
  • 4
    Dashes aren't generally a problem, but spaces are unless you double-quote all variable references (and may be causing this error as a secondary effect). See ["When should I double-quote a parameter expansion?"](https://stackoverflow.com/questions/55023461/when-should-i-double-quote-a-parameter-expansion). [shellcheck.net](https://www.shellcheck.net) is good at pointing out common mistakes like this. If it still doesn't work after fixing that, put `set -x` at the beginning to get an execution trace and see exactly what's happening that leads to the error. – Gordon Davisson Oct 04 '22 at 04:57
  • In your code, you would rather get an error with those files containing a space. Except inside `[[....]]`, you have to quote your variables (or, alternatively, use zsh instead of bash). – user1934428 Oct 04 '22 at 05:27
  • 2
    I took the liberty to remove the `sh` tag, since your question asks about bash. – user1934428 Oct 04 '22 at 05:28
  • @GordonDavisson I see. Thank you for directing me towards the correct direction. Im new to bash scripting, thus am still exploring the proper syntax and handling. – stranger Oct 04 '22 at 06:06
  • @user1934428 Thank you for the trouble! Will look into zsh as well – stranger Oct 04 '22 at 06:06
  • 3
    Try `[ -f "$FILENAME" ]`. – Renaud Pacalet Oct 04 '22 at 06:18
  • @RenaudPacalet Thanks for the answer! Figured out that causes my issue! – stranger Oct 04 '22 at 06:19

2 Answers2

1

After reading the post recommended by @GordonDavisson , the issue that I have faced is due to not quoting the variable that is storing my filename.

This is because "parameter expansions are subjected to both word-splitting and pathname expansion" [https://stackoverflow.com/questions/55023461/when-should-i-double-quote-a-parameter-expansion]. By quoting, it preserves the literal content of the parameter.

"General rule: quote it if it can either be empty or contain spaces (or any whitespace really) or special characters (wildcards). Not quoting strings with spaces often leads to the shell breaking apart a single argument into many" [https://stackoverflow.com/a/10067297/8638278]

logparser.log

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
}
stranger
  • 134
  • 1
  • 7
0
#!/bin/sh -x

find . | sed -n '/*.log/p' > stack

cat > ed1 <<EOF
1p
q
EOF

cat > ed2 <<EOF
1d
wq
EOF

next () {
[[ -s stack ]] && main
exit 0
}

main () {
line=$(ed -s stack < ed1)
msg "${RED}Parsing file: ${CYAN}$line"
ed -s stack < ed2
next
}

next

This exclusively uses files with a log extension, so you don't need to check.

petrus4
  • 616
  • 4
  • 7