bash - How to measure time of execution of each "sub-process" of a shell script -


i'm running program multiple times via script, need time of execution of each time run it. i'm trying use "time" function same way when use via terminal, doesn't work. here's code "time" function inside script:

#!/bin/sh time ./tree < in_1.in > out_1.out time ./tree < in_2.in > out_2.out time ./tree < in_3.in > out_3.out time ./tree < in_4.in > out_4.out time ./tree < in_5.in > out_5.out time ./tree < in_6.in > out_6.out time ./tree < in_7.in > out_7.out time ./tree < in_8.in > out_8.out time ./tree < in_9.in > out_9.out time ./tree < in_10.in > out_10.out 

note 1: without "time" before each line, script runs perfectly. if possible, i'd put every recorded time new file, tried using "echo", haven't had luck it.

note 2: there way make script run every file inside directory, without putting every , each 1 inside script?

there no time command in bourne shell (/bin/sh). run program #!/bin/bash.

you can write recorded times new files this:

time ./tree < in_1.in > out_1.out 2> time_1.time 

or if want time in outfile:

time ./tree < in_1.in &> out_1.out 

a basic way of running functionality each of .in files works loop , pathname expansion (using * wildcard):

for filename in *.in     time ./tree < "$filename" > "$( sed -e 's/in/out/g' <<< $filename )" done 

the expression "$( sed -e 's/in/out/g' <<< $filename )" expanded result of replacing in out in $filename. double quotes around prevent glob characters (*, ?) , newlines in filenames breaking code.

sed common tool substitution can written shorter using bash-only pattern substitution (note double / after first parameter equals g modifier in sed triggering substitution of all matches of pattern):

time ./tree < "$filename" > "${filename//in/out}" 

another common approach of executing commands set of files found in or under working directory using find -exec, may difficult in case due redirections.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -