开发者

Shell Script for checking log and running a program

I have this script that checks the last line of the file newlog.log and if it finds the word Stream closed there it should run the program test.jar because it would have stopped and hence written Stream closed in the log file.

#!/bin/sh
SUCCESS=0 
while (true);
do
sleep 5
tail -1 ~newlog.log | grep -q "Stream closed" .
if  [$? -eq 1]
then 
java -jar test.jar &
fi
done

The error I get is :开发者_Go百科

run.sh: 11: [1: not found

Please tell me if there's some syntax error or what?


Instead of using the Exit Status of the grep command, store the output in a variable and loop until it's length is non-zero.

Grep will return exit status of 0 even when it doesn't return any output.

var1=tail -1 ~newlog.log | grep -q "Stream closed"

until [ -n "${var1}" ];

do

  echo "Sleeping for next 5 seconds"

  sleep 5

done

java -jar test.jar &


The original error '[1' caused by the OP not putting spaces around the square brackets in the if statement.

It seems that the tail command is returning an exit code of 1. The if statement should be

if [ $? -eq 1 ]
then

...

and not

if [$? -eq 1]
then

....


I think you can use

tail -f file.log
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜