0

I am considering this answer with GNU time which I installed by brew.

data.txt

hello 
amigo
this line 3
and here we go 4
and 5 is here

Code in BSD time

time eval $(echo sed $(for i in 1 2 3 5; do echo "-e ${i}p"; done) -n data.txt )
hello 
amigo
this line 3
and 5 is her

real    0m0.008s
user    0m0.003s
sys 0m0.005s

Code in GNU time

$ gtime eval $(echo sed $(for i in 1 2 3 5; do echo "-e ${i}p"; done) -n data.txt )
gtime: cannot run eval: No such file or directory
Command exited with non-zero status 127
0.00user 0.00system 0:00.00elapsed ?%CPU (0avgtext+0avgdata 1228800maxresident)k
0inputs+0outputs (0major+93minor)pagefaults 0swaps

and with quotes

gtime eval '$(echo gsed $(for i in 1 2 3 5; do echo "-e ${i}p"; done) -n data.txt )'
gtime: cannot run eval: No such file or directory
Command exited with non-zero status 127
0.00user 0.00system 0:00.00elapsed 50%CPU (0avgtext+0avgdata 1196032maxresident)k
7inputs+0outputs (0major+91minor)pagefaults 0swaps

Based on Matthieu's comment, the gtime is looking for a eval bin but it is built-in bash command. Command which eval gives nothing.

Why GNU time cannot run eval?

  • I'm afraid I don't have a Mac OS system with GNU time installed to hand, but the GNU time call works fine on a Linux system (ie it's able to time a shell builtin, which eval is, correctly). Strange. – mjturner Jul 6 '15 at 9:11
  • 2
    The problem is that gtime seems like to be looking for a eval bin but its a built-in bash command. – Matthieu Riegler Jul 6 '15 at 9:53
  • @MatthieuRiegler Yes, which eval gives nothing. – Léo Léopold Hertz 준영 Jul 6 '15 at 10:29
  • @Masi What does type eval give you? What is your shell defined as (echo $SHELL)? – mjturner Jul 8 '15 at 9:39
2

gtime can't execute/time commands definied internally by the shell. Commands are executed using the exec() system call, but the system call basically only knows about commands with binaries (e.g. in /usr/bin). Builtin commands are included in the shell binary, there is no way for exec() to execute them.

You need to either find a way to execute the command without relying on shell builtins or use the internal time command of your shell. One option might (should work, but I have no means of testing it right now) be to run a new shell within gtime

gtime bash -c 'eval $(echo sed $(for i in 1 2 3 5; do echo "-e ${i}p"; done) -n data.txt)'

The timing will be slightly off because the time required to run a new shell is included, but you can measure that seperately by running gtime bash -c ':'.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .