Pass the value from pipe


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass the value from pipe
# 1  
Old 04-05-2015
Pass the value from pipe

am trying to pass the date calculated to variable so that i can use the same for my further reporting and tracking of the changes as per the variable value.

System = SunOS
Release = 5.9
KernelID = Generic_122300-61

Code:
date '+%m %d %Y' |
{
read MONTH DAY YEAR
DAY=`expr "$DAY" - 1`
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
YESTERDAY=${MONTH}/${DAY}/${YEAR};export YESTERDAY
} | YDAY=${YESTERDAY};export YDAY
echo $YDAY

OUTPUT in debug mode
Code:
+ date +%m %d %Y
+ read MONTH DAY YEAR
YDAY=
+ export YDAY
+ echo
+ expr 05 - 1
DAY=4
vardt=0
+ [ 0 -ne 0 ]
YESTERDAY=04/4/2015
+ export YESTERDAY

Request to help me please and thanks in advance.
# 2  
Old 04-05-2015
Just as date printed three values that you used read to import into the second stage of your pipeline, you need to print the results you want to export from the second stage of your pipeline and read the results in the third stage of your pipeline. The third stage of your pipeline is not in a subshell environment of the execution environment of the second stage of your pipeline; so variables exported in the second stage of your pipeline are not visible in the third stage of your pipeline.

Code:
date '+%m %d %Y' |
{
read MONTH DAY YEAR
DAY=`expr "$DAY" - 1`
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
echo "${MONTH}/${DAY}/${YEAR}"
} |
{
read YDAY
export YDAY
echo $YDAY
}


Last edited by Don Cragun; 04-06-2015 at 05:34 PM.. Reason: Put export and echo at end of script in a subshell.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-06-2015
That works, but only in ksh, which i didn't see the requestor mention. In other shells, the third stage is executed in a subshell, and the value read will not be available in the calling shell. You could use read with bash's process substitution here, or a variable assignment with command substitution.
# 4  
Old 04-06-2015
Quote:
Originally Posted by RudiC
That works, but only in ksh, which i didn't see the requestor mention. In other shells, the third stage is executed in a subshell, and the value read will not be available in the calling shell. You could use read with bash's process substitution here, or a variable assignment with command substitution.
Ouch. Yes. I intended to include an additional set of braces or parentheses making the last three lines all be a subshell as the 3rd stage of the pipeline. I will update post #2 momentarily.
# 5  
Old 04-07-2015
If you want the variable in the main shell (instead of a subshell)
then define a function:
Code:
yday() { 
    date '+%m %d %Y' | { 
        read MONTH DAY YEAR;
        DAY=`expr "$DAY" - 1`;
        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;
        echo "${MONTH}/${DAY}/${YEAR}"
    }
}
# main shell
YDAY=`yday`
echo $YDAY

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 04-08-2015
That did the trick Smilie

Thanks so much for the help and explanations guys, that is GREATLY appreciated! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cat a script, pipe it, then pass arguments to it?

suppose i have a perl script that is normally run this way: ./checkdisk.pl -H hostname -w 40 -c 80 but, for whatever reason, i cannot run the script directly as it should. But i can cat it through pipe. How can i pass the arguments "-H hostname -w 40 -c 80"? so this is what i'm doing,... (6 Replies)
Discussion started by: SkySmart
6 Replies

2. Shell Programming and Scripting

Function in one-linef and pass arguments in a pipe

I need to declare a function, this function will contain a script, this script cannot be in a file but must be piped. and then, for the script to run, i need to pass arguments to it. everything has to be on one line. so i'm basically looking for a one-liner here's what i'm doing: myfunc ()... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

How to pass parameter to pipe command row-wise?

Hi, I have a list of parameter in a file. I want to pass them one by one to piped command and syntax is like <command> <parameter> <command continues> How to achieve that ? Thanks (1 Reply)
Discussion started by: ezee
1 Replies

4. Shell Programming and Scripting

How to ignore Pipe in Pipe delimited file?

Hi guys, I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file for eg: file 1: xx|yy|"xyz|zzz"|zzz|12... using below awk command awk 'BEGIN {FS=OFS="|" } print $3 i would get xyz But i want as : xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

5. Shell Programming and Scripting

Replace pipe with Broken Pipe

Hi All , Is there any way to replace the pipe ( | ) with the broken pipe (0xA6) in unix (1 Reply)
Discussion started by: saj
1 Replies

6. Shell Programming and Scripting

How can I use pipe

Hi, guys: I am working on my shell using c. How can I use pipe to implement the following? ls -l 1>> | grep hellp 1<< 2>> | less 2<< (the output of ls goes to grep, and the output of grep goes to less) Thanks Please use and tags when posting code, data or logs etc. to preserve... (1 Reply)
Discussion started by: tomlee
1 Replies

7. Shell Programming and Scripting

How to pass a field from awk in a pipe?

Thanks in advance : ) I try for a long time searching for a way to split a large gzip csv file into many gzip files (except for the last sub-file which is to joint the next big file's children.) All the subfiles are to be named by the field. But I only managed to split them into the... (9 Replies)
Discussion started by: Kingsley
9 Replies

8. Shell Programming and Scripting

pass more than one input with pipe ("|")

Hi everyone, Is there any way to use the pipe "|" operator to pass more than one input? For instance do: (command1) (command2) | command3 with command 3 taking 2 parameters? or maybe using ``s? Thanks! Anthony (4 Replies)
Discussion started by: anthony.cros
4 Replies

9. UNIX for Advanced & Expert Users

pf not working properly even with only "pass in all" and "pass out all" rules

i have two rules in my pf.conf file, "pass in all" and "pass out all" i was having issues with getting pf working to begin with, so i went with starting from nothing and working on up. i have an ultrasparc ultra1 200e, with an added 4-port fast ethernet sbus card, running "3.4 GENERIC#85... (4 Replies)
Discussion started by: xyyz
4 Replies

10. Programming

pipe help

i made a lot of processes. here is the code: main() { printf("\nEnter K="); scanf("%d",&k); printf("Enter L="); scanf("%d",&l); printf("\nFather id=%d\n",getpid()); x=0; makechild(); sleep(2); return 1; } int makechild() { for(q=1;q<=k;q++) { if(f=fork()) { ... (5 Replies)
Discussion started by: bb666
5 Replies
Login or Register to Ask a Question