jenkins: scripts: jenkins-sudo-grep.sh: Fix case for empty OLDLOGFILE

It's possible for OLDLOGFILE to not exist since the LOGFILE may not have
rotated yet. However, this leads to broken behavior in the CI like the
following one:

2017-07-18 07:43:14.996187 | ls: cannot access '/var/log/messages-*': No such file or directory
2017-07-18 07:43:14.997653 | stat: missing operand
2017-07-18 07:43:14.997688 | Try 'stat --help' for more information.

So even though the OLDLOGFILE variable is empty ('ls' returned nothing)
the following branch is taken which is definitely not what we expected.

if [ -f $OLDLOGFILE ]; then...

The reason for that, is that the branch is really ends up being

"if [ -f ]; then" which in turn is "if [ -n -f ]; then"

So the branch really checks if the '-f' string is non-empty.

There are possible ways to fix that, but the simplest one is to simply
quote the "OLDLOGFILE" variable so the branch behaves as we expected it
to.

Change-Id: I8e928fc1ba601ca4cf3dfaa02806bdb9dfefc61b
This commit is contained in:
Markos Chandras 2017-07-18 08:58:42 +01:00
parent 0b4140565d
commit 01848c95bb

View File

@ -37,7 +37,7 @@ case "$1" in
pre)
rm -fr /tmp/jenkins-sudo-log
mkdir /tmp/jenkins-sudo-log
if [ -f $OLDLOGFILE ]; then
if [ -f "$OLDLOGFILE" ]; then
stat -c %Y $OLDLOGFILE > /tmp/jenkins-sudo-log/mtime-pre
else
echo "0" > /tmp/jenkins-sudo-log/mtime-pre
@ -46,7 +46,7 @@ case "$1" in
exit 0
;;
post)
if [ -f $OLDLOGFILE ]; then
if [ -f "$OLDLOGFILE" ]; then
stat -c %Y $OLDLOGFILE > /tmp/jenkins-sudo-log/mtime-post
else
echo "0" > /tmp/jenkins-sudo-log/mtime-post