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