0

In a command where we have lot of pipes redirecting their outputs further, is there any way to get to know the value of echo $? till half of it's execution or till certain number of pipes, as opposed to the overall status of the whole command's? To exemplify, I'm running below command to get the time stamp of the file to be converted in it's epoch time as,

`ls -lrt --time-style=+"%b %d %Y %H:%M:%S" /some/path/*.dat|head -1|tr -s " "|cut -d " " -f 9|date --date - +%s`

But not getting what I want as I checked manually by splitting it in two halves as,

var1=`ls -lrt --time-style=+"%b %d %Y %H:%M:%S" /some/path/*.dat|head -1|tr -s " "|cut -d " " -f 9`
var2=`date --date=$var1 +%s`

In the first unbroken command I'm still getting an output without any error and is not something I'm expecting, but I don't even want to use two commands too. Could anyone suggest anyway to do so or where exactly I'm erring?

Update: Why would the below produce today's midnight's epoch(what happens if date's been given - to get the earlier piped output - though it doesn't work that way) The first question remains unanswered too.

-bash-3.2$ ls -lrt --time-style=+"%b %d %Y %H:%M:%S" *.dat|head -1|tr -s " "|cut -d " " -f9|date --date - +%s; date +%s
1429056000      #Midnight's
1429093077      #Current

PS: It was Not the current epoch.

2
  • By the way, the epoch for Unix-type systems is midnight on January 1st 1970. Everything else is a time. I think you've possibly got a confusion of the meaning of the word.
    – roaima
    Apr 15, 2015 at 11:15
  • Nope, know that, just not getting how it is giving 1429056000 Apr 15, 2015 at 11:50

2 Answers 2

1

In a command where we have lot of pipes redirecting their outputs further, is there any way to get to know the value of echo $? till half of it's execution or till certain number of pipes, as opposed to the overall status of the whole command's?

In bash, there's a PIPESTATUS variable, which is an array containing the exit status of each command in the most recent pipeline.

$ ls -lrt --time-style=+"%b %d %Y %H:%M:%S" /bin/*|head -1|tr -s " "| \
  cut -d " " -f 9|date --date - +%s
1429070400
$ echo ${PIPESTATUS[@]}
141 0 0 141 0
$ kill -l `expr 141 - 128`
PIPE

This tells us that the ls and cut commands exited with a SIGPIPE, which is expected because their output was not completely consumed by the next command in the pipeline.

In the first unbroken command I'm still getting an output without any error and is not something I'm expecting.

That's because the date command is working as it expects, but, I think, not the way you are expecting.

date --date - does not read the date from stdin; instead it uses - as the date string. I can't see where the meaning of a lone - is documented, but it appears to be the same as 0 or 0000, meaning "midnight of the current day".

1

You have this command

ls -lrt --time-style=+"%b %d %Y %H:%M:%S" /some/path/*.dat|head -1|tr -s " "|cut -d " " -f 9|date --date - +%s

The problem with this is that date does not expect its parameters via stdin, so you need to split it into two parts. You've already attempted this in the second part of your Question:

var1=$(ls -lrt --time-style=+"%b %d %Y %H:%M:%S" /some/path/*.dat|head -1|tr -s " "|cut -d " " -f 9)
echo "var1='$var1'"    # eg var1='11:37:39'
var2=$(date --date="$var1" +%s)
echo "var2='$var2'"    # eg var2='1429094259'

However, when you run the date command, it's giving you the time (11:37:39 - 1429094259) for today (compare it to the value for var2 with my 11:04:45 - 1429092286 and you'll see they are very similar).

Perhaps stat --format '%Y' /tmp/path/*.dat would be easier?


Some answers to specific questions

Is there any way to get to know the value of echo $? [midway through a pipe]? Not directly, no. However, you could either split the pipeline into two parts with a temporary file to hold the intermediate results, or save off the $? value as you go.

cmd1 | ( cmd2; SS=$?; echo $SS >/tmp/cmd2.ss; exit $SS ) | cmd3
cmd2_status=$(</tmp/cmd2.ss)

How can I do what I want in a single pipeline? You can evaluate the time and pass that to date, something like this

date --date=$( cmd1 | cmd2 | cmd2 ) +%s

Why does date --date=- produce the time at midnight? The date command is trying to do something meaningful with the invalid date "-". It happens that this ends up being interpreted as "midnight today". I wouldn't bank on it, though; if you really want midnight I'd go for an explicit date --date='00:00'

4
  • Have confirmed that the output I'm getting is not the current epoch too by running both the epoch of earlier(wrongly put) & the current epoch as, ls -lrt --time-style=+"%b %d %Y %H:%M:%S" *.dat|head -1|tr -s " "|cut -d " " -f9|date --date - +%s; date +%s which produced outputs as 1429056000 #This is today's midnight's epoch & 1429093077 #This is current epoch Apr 15, 2015 at 10:30
  • @Keys I can't make head nor tail of that, sorry. Please would you update your question with your new information so it's easier (for everyone) to read.
    – roaima
    Apr 15, 2015 at 10:40
  • Added an update. Apr 15, 2015 at 10:50
  • @Keys likewise.
    – roaima
    Apr 15, 2015 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.