Hi Experts,
Am writing a code which need to check for the previous day date and pickup the file as per the previous day date.
Problem: Why variable "YDATE" is empty ?
O/S: RHEL 5.6
Shell: BASH
Desired O/P: ls -lrt /opt/test/user/atsuser.NHU/out/demon.08272017
When I checked the permission, user is having the permission to check the file and file is present on the desired location.
Code:
#!/bin/bash
set -x
YESTERDAY_DATE()
{
date '+%d %m %Y' |
{
read DAY MONTH YEAR
DAY=`expr "$DAY" - 1 + 100 | cut -c2-3`
case "$DAY" in
0)
MONTH=`expr "$MONTH" - 1`
case "$MONTH" in
0)
MONTH=12
YEAR=`expr "$YEAR" -1`
;;
esac
DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac
vardt=$?
if [ $vardt -ne 0 ];then
echo "ERROR DURING SPOOLING PREVIOUS DAY DATE."
exit 1
fi
YDATE=${MONTH}${DAY}${YEAR}
}
}
YESTERDAY_DATE
echo $YDATE
echo ${YDATE}
ls -lrt ${PATH}/out/demon.${YDATE}
Output of the same is as below.
+ YESTERDAY_DATE
+ date '+%d %m %Y'
+ read DAY MONTH YEAR
++ expr 28 - 1 + 100
++ cut -c2-3
+ DAY=27
+ case "$DAY" in
+ vardt=0
+ '[' 0 -ne 0 ']'
+ YDATE=08272017
+ echo
+ echo
+ ls -lrt /opt/test/user/atsuser.NHU/out/demon.
ls: cannot access /opt/test/user/atsuser.NHU/out/demon.: No such file or directory