Can anyone help me with this, please:
This code works as intended:

eval "$(date "+yr=%Y mh=%m dy=%d hr=%H mt=%M sd=%S")"
echo "year=$yr ; month=$mh ; day=$dy ; hour=$hr ; minute=$mt ; second=$sd"

It gives output

year=2012 ; month=02 ; day=09 ; hour=14 ; minute=30 ; second=17

on stdout.
This code should work similarly:

eval "$(command time -f "real=%e user=%U system=%S" ls > /dev/null)"
echo "real = $real ; user= $user ; system = $system"

But it gives output

real=0.00 user=0.00 system=0.00
real =  ; user=  ; system =

Variables are not populated for some reason.
The only difference I could spot is that the first line real=0.00 user=0.00 system=0.00 (coming from time ) goes to stderr unlike output from date that goes to stdout and isn't even printed. So I changed the script:

eval "$( (command time -f "real=%e user=%U system=%S" ls > /dev/null)>&1 )"
echo "real = $real ; user= $user ; system = $system"

but result was same.

Recommended Answers

All 2 Replies

Do it like

#!/bin/bash
eval "$(date "+yr=%Y mh=%m dy=%d hr=%H mt=%M sd=%S")"
echo "year=$yr ; month=$mh ; day=$dy ; hour=$hr ; minute=$mt ; second=$sd"

eval "$(command time -p sleep 2 2>&1 | awk 'BEGIN{ORS=" "}{print $1"="$2}')"
echo "real = $real ; user= $user ; system = $sys"

Output:
$ ./myscript
year=2012 ; month=02 ; day=10 ; hour=22 ; minute=48 ; second=39
real = 2.00 ; user= 0.00 ; system = 0.00
$

Just realized my mistake. I was redirecting stdout to stdout (>&1), instead of stderr to stdout (2>&1). Correct code would be:

eval "$( (command time -f "real=%e user=%U system=%S" ls > /dev/null) 2>&1 )"
echo "real = $real ; user= $user ; system = $system"

Thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.