Unable to capture value from function


 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Unable to capture value from function
# 1  
Old 08-28-2017
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:
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.

Code:
+ 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


Last edited by pradeep84in; 08-28-2017 at 10:05 AM.. Reason: updated with the O/S, problem, desired result, etc.
# 2  
Old 08-28-2017
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 reading 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.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-28-2017
Hello Experts,

Problem is fixed and now it's working, am posting the new code.

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.
# 4  
Old 08-28-2017
How about
Code:
date
Mo 28. Aug 15:30:26 CEST 2017
date -d-1day
So 27. Aug 15:30:29 CEST 2017

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

How to Dynamically Pass Parameter to plsql Function & Capture its Output Value in a Shell Variable?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: #! /bin/ksh v="ORG_ID" ... (2 Replies)
Discussion started by: sujitdas2104
2 Replies

2. Shell Programming and Scripting

How to capture system() function output in variable

How to capture system() function output in awk variable and the print that awk variable..... (8 Replies)
Discussion started by: bharat1211
8 Replies

3. Shell Programming and Scripting

Unable to run function

Hi, I have a separate file with a ksh function in it. When I try to run it, I get an error about permissions: user@~/scripts/functions$ ksh f_fill_testfunc ksh: f_fill_testfunc: cannot open user@~/scripts/functions$ ls -l batch_functions/f_fill_testfunc -rwxr-xr-x 1 user user 1105 Aug... (18 Replies)
Discussion started by: Subbeh
18 Replies

4. Programming

unable to send a char parameter from main to a function

why does this not work? #include <stdio.h> #include <stdlib.h> char getFileMode(char charChanger) { char filetype; /*var to hold the value to be returned*/ filetype = charSetter; /*set filetype to "l" if it is a symlink*/ return filetype; } int main(void){ char... (8 Replies)
Discussion started by: bluetxxth
8 Replies

5. Programming

unable to use a function to crate a joinable thread

In my program, threads may be created when some events trigger. So I can't create threads on initialization. Theremore,I write a createThread() function to create thread. However, it is blocking at first call and don't run anymore? why? #include <pthread.h> #include <stdio.h> #include... (4 Replies)
Discussion started by: sehang
4 Replies

6. Shell Programming and Scripting

unix capture oracle function error

Hi, I want to execute an oracle function from unix script so for that I created a sample oracle function as below: create or replace function test_fn(test_date out varchar2) RETURN varchar2 IS BEGIN select to_char(sysdate,'DD-MON-YY') into test_date from dual; return test_date;... (5 Replies)
Discussion started by: dips_ag
5 Replies

7. Shell Programming and Scripting

how to capture oracle function returning 2 values in unix

i have an oracle function which returns two values, one is the error message if the function encounters anything and another one which returns a number i need to capture both and pass it on to unix shell script how to do it (2 Replies)
Discussion started by: trichyselva
2 Replies

8. Shell Programming and Scripting

unable to return a decimal value from a function

Hi Guys, I am unable to return a decimal value from a function to the main script. I am getting the correct value in the function but when it is returning to the main script using "return" it is coming as 0. Below is my code. read VALUE_V fun_FATHERID fun_SONID fun_TOPID fun_RTYPE <... (2 Replies)
Discussion started by: mac4rfree
2 Replies

9. UNIX for Advanced & Expert Users

Unable to execute a function using trap

I have a script A which calls script B. I wrote a function in script A to be executed when Kill command is issued for script A and I invoke that function using the trap command.The function identifies all child process running under script A (in this case script B) and kills the child process and... (3 Replies)
Discussion started by: smohandass
3 Replies

10. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies
Login or Register to Ask a Question