Script not running

hi

i have a alarm file mentioned as below

alarm_log.15122017

i want to fetch failure detail but script is not working .
kindly let me now where is the mistake

 
 #!/bin/bash
cd /scripts/
DATE=`date "+ %d%m%Y"`
ls -lrt | grep -i "alarm_log.$DATE"
cat alarm_log.$DATE |grep -B 1 "failure"| tail -2 >v1.txt
if [ -s v1.txt ]
then
echo "sync fail in ${HOSTNAME}
else
echo "no issue"
fi
 

Line one may have a leading space.
the directory /scripts may not be accessible.
Line 4 the file alarm.log.$(DATE) might not exist or be readable
Line 8, HOSTNAME is not defined. You probably mean $(hostname)

Hi,

Can you provide more detail on how exactly the script is not working, please ? Are you getting an error, and if so, what is the full text of the error ? If you are not getting an error as such, then if you could explain exactly what is going wrong, perhaps someone here will be able to assist you further. But either way, we'll need to know what the problem and nature of the failure actually is first.

HI

I am getting below error

 
 ./c.sh 
cat: af02_alarm_log.: No such file or directory
cat: 15122017: No such file or directory
no issue
 

it seems to me that the line

cat alarm_log.$DATE |grep -B 1 "failure"

is not treating it as a single file but I treating as 2 file.
can anyone help me

The variable DATE has a space in it. You are setting it as

DATE=`date "+ %d%m%Y"`

which includes the space after the +

Try using

DATE=`date "+%d%m%Y"`

instead. Can you spot the tiny difference? Your shell can, hence the failures.

Robin

Amen to what rbatte1 said. Furthermore the line is a "useless use of cat ", as the experts call it. This:

grep -B 1 "failure" "alarm_log.$DATE"

Does the same in less processes and with less taxing the system.

I hope this helps.

bakunin

thx a lot rbatte1 .