coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: date--date accept DATESTR from stdin?


From: Trey Blancher
Subject: Re: date--date accept DATESTR from stdin?
Date: Wed, 2 Jun 2021 21:35:51 -0400

Bernhard,

Thank you for your reply. I did see that, sometimes `chage` won't have a date. But the system I actually run it from sets the expiration by default due to system policy, so I'm not likely to see it say,"never". I'd have to go out of my way to set it to not expire. But good to know for the general purpose sense.

Trey Blancher
trey@blancher.net

On Wed, Jun 02, 2021 at 10:12:31PM +0200, Bernhard Voelker wrote:
On 6/2/21 5:22 AM, Trey Blancher wrote:
I ran across something that didn't work for me, but I see no reason
why it shouldn't.  I've tried this on a version of RHEL 7.6, as well as an
up-to-date Arch Linux (coreutils 8.32).  I have a script/command pipeline that
extracts the password expiration date from `chage`, and I tried to feed it into
`date`:

chage -l ${USER} | grep 'Password expires' | awk -F':' '{print $2}' | date -d - 
+%b-%d-%Y>

But this didn't work, it just printed today's date.  I guess this is because a
bare dash/hyphen as a date specifier is ignored.  I was able to workaround
the problem by loading the `chage` call into a subshell:

date -d "$(chage -l ${USER} | grep 'Password expires' | awk -F':' '{print 
$2}')" +%b-%d-%Y

Reading the man/info pages for `date`, it doesn't look like -d/--date accepts
the date DATESTR from standard input.  It also doesn't look like a herestring
will work, either.  Other coreutils programs do accept a single hyphen
('-') to mean read from standard input, so that's why I figured it would work.

As i was writing this, I saw the -f/--file option, which does allow the
dash/hyphen to mean read from standard input.  Here's what just worked for me:

chage -l ${USER} | grep 'Password expires' | awk -F':' '{print $2}' | date -f - 
+%b-%d-%Y

Is that the answer?

Yes, the -f, --file option is the way to tell date(1) to read from standard 
input:

https://www.gnu.org/software/coreutils/manual/html_node/Options-for-date.html#Options-for-date

 ‘-f datefile’
 ‘--file=datefile’
   Parse each line in datefile as with -d and display the resulting date and 
time.
   If datefile is ‘-’, use standard input.  [...]

While the feature also aims at processing many dates at a time, it is perfectly 
legit
to use it for one date string only to avoid a sub-shell (by possibly adding a 
pipe).

BTW: 'chage -l $USER' doesn't seem to reliably print a valid date string as 
value
for "Password expires".  At least, also "never" seems to be common.

 $ chage -l $USER | grep 'Password expires' | awk -F':' '{print $2}' | date -f 
- +%b-%d-%Y
 date: invalid date ' never'

Therefore, your script should cater for that as well.

Would it even make sense to read -d/--date from stdin,
knowing that -f/--file can do it?

I don't think so: the use of -d is fine if the date string is in a variable, and
the -f option is fine when that string is coming from standard input.
FWIW: the "herestring" you mentioned above is also regular data read from 
standard
input, as far as the invoked command is concerned.

Have a nice day,
Berny




reply via email to

[Prev in Thread] Current Thread [Next in Thread]