Unable to capture value from function

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

Please become accustomed to provide decent context info of your problem.
It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

Hadn't it been nice had you presented your problem in sufficient verbosity? Am I guessing right you want to know why the YDATE variable is empty / unassigned? That's because the read ing from the pipe is done in a subshell and thus not passed back to the parent.

If people knew what your system is, proposals could be posted to simplify your shell function, or make it more efficient resp.

1 Like

Hello Experts,

Problem is fixed and now it's working, am posting the new 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}
                echo ${YDATE}
        }
}

Y_DT=`YESTERDAY_DATE`

setted the echo which is passing the value of the variable to outside and calling the function and capturing the value of the same in the new variable.

How about

date
Mo 28. Aug 15:30:26 CEST 2017
date -d-1day
So 27. Aug 15:30:29 CEST 2017