1

I can't find the reasons why my case statement doesn't work when looking tail output.

tail -F -n1 /var/log/pihole.log |
while read input; do
 echo "$input" | hexdump -C            # just to physically compare the output
 case $input in
   cached|blacklisted|blocked)
     echo "We have a match!";;
   *)
     echo "No match!"
 esac
done

This always returns No match!, even if the strings are in the $input.

:~ $ ./pihole_test.sh
00000000  4a 61 6e 20 31 20 31 31  3a 35 35 3a 35 38 20 64  |Jan 1 11:55:58 d|
00000010  6e 73 6d 61 73 71 5b 36  39 36 5d 3a 20 65 78 61  |nsmasq[696]: exa|
00000020  63 74 6c 79 20 62 6c 61  63 6b 6c 69 73 74 65 64  |ctly blacklisted|
00000030  20 70 6c 61 79 2e 67 6f  6f 67 6c 65 2e 63 6f 6d  | play.google.com|
00000040  20 69 73 20 30 2e 30 2e  30 2e 30 0a              | is 0.0.0.0.|
0000004c
No match!
tripleee
  • 175,061
  • 34
  • 275
  • 318
lz_linux
  • 13
  • 3
  • 1
    Edit the question to include example input so we know what we are working with – hardillb Jan 01 '22 at 09:27
  • Replace `echo $input` with `echo "$input" | hexdump -C` and add output to your question (no comment here). – Cyrus Jan 01 '22 at 09:43
  • Why do you need the loop though? Simply `tail -F -n1 /var/log/pihole.log | grep -E 'cached|blacklisted|blocked'` will extract any matching lines. Maybe `grep -E 'cached|blacklisted|blocked|$'` --color=yes` if you want to see all lines but highlight the matches. – tripleee Jan 01 '22 at 10:33

1 Answers1

1

Replace

cached|blacklisted|blocked)

with

*cached*|*blacklisted*|*blocked*)

to match substrings.

Cyrus
  • 84,225
  • 14
  • 89
  • 153