0

For some strange reason, the following script is throwing the error - line 28: syntax error: unexpected end of file


TYPEPATTERN=("LogName=Security", "auditd", "hostd")

###### MAIN

for gzip in $(find . -type f -name "*.gz");
do
    for pattern in ${TYPEPATTERN[@]}; 
    do

        zcat $gzip | grep $pattern

        #Add to list if matches search
        if [ $? == 0 ]
        then
            if pattern == "LogName=Security"
            then
                mv $gzip /tmp/recover/windows
            else                
                mv $gzip /tmp/recover/unix
            fi
        fi
    done
done
spectrum
  • 109
  • 6
  • 4
    Run your code through http://shellcheck.net/ and fix the issues it identifies. – Charles Duffy Mar 10 '21 at 16:51
  • ...btw, that set of issues _doesn't_ include anything that would directly cause an "unexpected EOF", but if you run it against your whole program (not just the snippet given here), that'll presumably give you more comprehensive results. Note that the line number is often less useful than you'd expect in that class of situation. – Charles Duffy Mar 10 '21 at 16:52
  • ...that said, if you get that exact error and this _is_ your whole code, it's likely your file may have DOS newlines instead of UNIX ones. DOS newlines have a CR before each LF, which UNIX doesn't recognize, so f/e, instead of `do` it would see `$'do\r'`, and instead of `done` it would see `$'done\r'`, so that's how your syntax is seen as invalid. We can't tell if that's the case from here, though, because those characters don't get copied/pasted and aren't there in your question. – Charles Duffy Mar 10 '21 at 16:54
  • ...if that _is_ the case, the correct duplicate to close this question with is [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – Charles Duffy Mar 10 '21 at 16:55
  • 2
    `if pattern == "LogName=Security"` should probably be `if [ pattern == "LogName=Security ]"` – axiac Mar 10 '21 at 17:03
  • Just use `for gzip in *.gz`. `find` is completely unnecessary and won't work when the filenames contain whitespaces. – M. Nejat Aydin Mar 10 '21 at 17:31
  • @axiac Not quite. It should use `=` instead of `==`, because that's the POSIX and portable comparison operator. Other shells may complain or just not work. – Jens Mar 10 '21 at 17:33
  • @axiac if pattern == "LogName=Security" should probably be if [ pattern == "LogName=Security ]" - this fixed the issue. Thanks a lot. – spectrum Mar 10 '21 at 17:37
  • @CharlesDuffy - thanks for the shellcheck.net – spectrum Mar 10 '21 at 17:38
  • @M.NejatAydin - basically i have 1000+ gz file in 100+ folders. I want to go through all the files to find the pattern. – spectrum Mar 10 '21 at 17:40
  • @axiac, certainly not -- that's always false. `if [ "$pattern" = LogName=security ]`, maybe. You don't need quotes on the right-hand side, you _do_ need them on the left hand side, and to look at a variable one needs `$`. Also, `==` isn't guaranteed to work in `[ ]`; `=` (only one character!) is the only POSIX-standardized string comparison operator. – Charles Duffy Mar 10 '21 at 18:11
  • @M.NejatAydin, ...I disagree with saying that `find` doesn't work with filenames containing whitespace; it's `for element in $(anything)` that doesn't work with values containing characters in IFS (thus, by default, whitespace) or values that can be expanded as globs. It's perfectly possible to use `find` with whitespace-containing filenames, one just needs to use better practices to process its output -- as discussed in sections 5 through 7 of [Using Find](https://mywiki.wooledge.org/UsingFind#Actions). – Charles Duffy Mar 10 '21 at 18:13
  • @CharlesDuffy My comment was in the context of the code given, that is, `for gzip in $(find . -type f -name "*.gz")`. – M. Nejat Aydin Mar 10 '21 at 18:15
  • _nod_; point being that it's important to make it clear how the context impacts the advice, since we're writing for a reader who's a beginner and so could take "find won't work when X" to _mean_ "find won't work when X", instead of "the way you're currently using find won't work when X". – Charles Duffy Mar 10 '21 at 18:16
  • @spectrum `"basically i have 1000+ gz file in 100+ folders. I want to go through all the files to find the pattern."`: You can use `for gzip in **/*.gz` after having set `globstar` option by `shopt -s globstar` (you'll need bash version 4.0 or newer) – M. Nejat Aydin Mar 10 '21 at 18:24

0 Answers0