2

I'm trying to make a small script which watches the latest log file in a folder and grabs the chat from the log, which i'll then display in a tmux panel but for some reason it can't find the file while it does exist

FILENAME=server_logs/$(ls -t1 server_logs | head -n 1)
watch -n3 'grep "\] " '$FILENAME' | tail -n15' 

When i run the file it gives me this grep: server_logs/server_log_09_18_12.txt : No such file or directory

But if i use watch -n3 'grep "\] " server_logs/server_log_09_18_12.txt | tail -n15' it does work. So my question is why can't it find the file?

  • 1
    How are you running the script? If you're running it from cron for example, the start part his perhaps not what you thought. – BugFinder Sep 18 '12 at 08:22
  • I'm using "bash watch.sh" to run it, through SSH. – MMartinezMusterd Sep 18 '12 at 08:24
  • While I'd normally just use `tail -f ... | grep ...` for that, your command looks just fine. Is that your full script? – Shawn Chin Sep 18 '12 at 08:47
  • Does it run correctly in the script when you specify the full path to `server_logs/*`? – imp25 Sep 18 '12 at 08:48
  • Two ideas: simplify the quoting a bit (`watch "cat '$FILENAME' | grep '\] ' | tail -n15"`) and use full paths (`$(readlink -f $(ls -t1 ... | head ))`). There is nothing really wrong with your code though. – gvalkov Sep 18 '12 at 08:56
  • @ShawnChin it is the entire code yeah, i've also tried specifying the whole path. I'll try your ideas gvalkov. – MMartinezMusterd Sep 18 '12 at 09:22
  • Also do check the if you have the right file access permissions. – askmish Sep 18 '12 at 10:25

2 Answers2

0

watch -n3 'grep "] " '$FILENAME' | tail -n15'

has two sets of ' quote signs. You want to escape the inner ' signs.

pitseeker
  • 2,535
  • 1
  • 27
  • 33
0

I found out why it didn't work, $FILENAME had some spaces at the end which caused the file not found error, so i used this:

FILENAME="${FILENAME#"${FILENAME%%[![:space:]]*}"}"   # remove leading whitespace characters
FILENAME="${FILENAME%"${FILENAME##*[![:space:]]}"}"   # remove trailing whitespace characters 

Which i found at another Stackoverflow question.

Thanks for the responses.

Community
  • 1
  • 1