1

This command work perfectly in my shell and I want to write it inside a bash script but it doesn't work anymore :

make -s clean > /dev/null; { time make -j -s gallery > /dev/null; } 2>&1 | grep real | sed 's/^.*m//;s/.$/ /' > time_make 

The problem is the outpout of time.

In the shell this commande :

make -s clean > /dev/null; { time make -j 1 -s gallery > /dev/null; } 2>&1 | grep m > time_make

has for result :

real   0m2.127s
user   0m3.375s
sys    0m0.532s

That's good.

But in the script (#! /bin/sh):

The same command has for result :

3.36user 0.51system 0:02.08elapsed 186%CPU (0avgtext+0avgdata49100maxresident)k
0inputs+2384outputs (0major+114980minor)pagefaults 0swaps

How to get the value of "real" in the bash script?

I really don't understand why it's not the same output for the "time" command. Could anyone help me please?

Thanks in advance.

6
  • Try running /usr/bin/time explicitly. Commented Dec 17, 2015 at 1:38
  • ah.... it gives me the one I don't want. thank you, so the other one should be.... umh i tried the other given by "whereis time" but I didn't find a good one Commented Dec 17, 2015 at 1:43
  • Use which time to find out what is being used. It might be part of the shell, and not a command at all. Commented Dec 17, 2015 at 1:45
  • It gives me the one you said, Commented Dec 17, 2015 at 1:47
  • 1
    If you read the man-page for Bash you'll find that it provides its own time. There is only one option time -p for POSIX-style output. Maybe that's what you're after? Commented Dec 17, 2015 at 1:53

1 Answer 1

1

If you want to capture the time a command takes in bash, you can get a lot more control by doing something like this (assumes you're on a system with GNU time installed):

START=$(date +%s.%N)
# command you want to time goes here
END=$(date +%s.%N)
printf -v DELTA "%g" $(bc <<<"$END - $START")
echo "Command took ${DELTA} seconds"

You can actually format the output into hours, minutes, seconds etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.