1

I know that similar questions to this have been asked, but I haven't found one that addresses my situation closely enough...

I need to time a command in a bash script, similar to this:

mytime="$(time ( ls ) 2>&1 1>null)"
rm null

This works fine. But when I try to replace ls with something more complicated, I run into issues:

command="$reg 1d_euler_2nd.rg -nc $nc -ll:cpu $np -ns $nstop"
mytime="$(time ( $command ) 2>&1 1>null)"
rm null

where reg is an environment variable and nc, np, and nstop are all defined in my script. I get the following error when I try to use this code:

(standard_in) 1: syntax error
(standard_in) 1: illegal character: :
(standard_in) 1: illegal character: :
(standard_in) 1: illegal character: $
(standard_in) 1: syntax error
(standard_in) 1: illegal character: :
(standard_in) 1: syntax error
(standard_in) 1: syntax error
(standard_in) 1: illegal character: :

Any help would be greatly appreciated!

My apologies -- as I was trying to create a minimal working example, I realized that the problem was elsewhere in my code.

  • Do you do something with the file null before you delete it or do you just mix it up with /dev/null? What does "where reg is an environment variable" mean? What is the content? – Hauke Laging Aug 8 '17 at 21:05
  • Please post a complete, minimal example of the code that still experiences the problem. It's not clear right now what reg, nc etc. might contain. – Alexander Batischev Aug 8 '17 at 21:06
  • And please run your code through bash -vx script.sh and add the output to your question. I am not sure which program produces the error messages. – Hauke Laging Aug 8 '17 at 21:10
1

I am able to capture the output of time separately just fine with a subshell without using a variable to abstract a command. A simple example for brevity:

$ time ( echo "Hello" > example.out 2> example.err ) > time.out 2> time.err
$ ls *{out,err}
example.err example.out time.err    time.out
$ cat time.err

real    0m0.000s
user    0m0.000s
sys     0m0.000s
$ cat example.out
hello
  • Impressive. Is that a shell bug? :-o – Hauke Laging Aug 8 '17 at 21:13
  • Nope; that's the shell working as intended. time's diagnostic output goes to standard error, but if you don't want that commingled with the standard error of your actual job, you need to first capture its standard error, which in my example is taken care of in the redirects within the subshell. – DopeGhoti Aug 8 '17 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.