I have a shell script which logs the performance of various programs passed to it as arguments, to help me choose the most performant one. The relevant snippet:
#!/usr/bin/env sh
function cal_perf () {
real_t=0
user_t=0
sys_t=0
for (( trial=1 ; trial<=$3 ; ++trial )) ; do
shopt -s lastpipe
/usr/bin/time -f '%e:%S:%U' $1 $2 |& IFS=":" read real sys user
real_t=$(echo "$real + $real_t" | bc)
user_t=$(echo "$user + $user_t" | bc)
sys_t=$(echo "$sys + $sys_t" | bc)
done
real_t=$(echo "scale=2 ; $real_t / $3" | bc)
user_t=$(echo "scale=2 ; $user_t / $3" | bc)
sys_t=$(echo "scale=2 ; $sys_t / $3" | bc)
printf "%s\t%d\t%.2f\t%.2f\t%.2f\n" $2 $3 $real_t $user_t $sys_t >> timings_$(date '+%Y%m%d')
}
main
printf "program\t#trials\treal_time_am\tuser_time_am\tsys time_am\n" > timings_$(date '+%Y%m%d')
translator=$1
shift
while [ $# -gt 1 ] ; do cal_perf $translator $1 ${!#} ; shift ; done
It's supposed to be run on the command line as follows:
perf <translator_progam> <list_of_programs_to_compare> <number_of_trials>
...for instance: suppose I want to compare the performances of xip.py, foo.py, bar.py, bas.py, qux.py
--the net content of the working directory--and run them each for 50 times before generating the stats; I'd invoke the script as:
perf python *py 50
I think I am missing something obvious here, but when I invoke this script as bash $HOME/bin/perf ...
everything works as intended. However, the following two invocations fail (error attached):
perf ...
- or even placing it in the working directory and invoking as
./perf ...
changing the shebang to /usr/bin/env bash
solves this problem, but /usr/bin/sh
points to /usr/bin/bash
on my system.
for
loop is a bashism, it doesn't exist insh
. – choroba Oct 08 '21 at 15:27/usr/bin/sh
is a symlink to/usr/bin/bash
? – puwlah Oct 08 '21 at 15:30/usr/bin/env sh
resolves to/usr/bin/sh
? – choroba Oct 08 '21 at 15:40sh
, Bash enters POSIX mode after reading the startup files." – glenn jackman Oct 08 '21 at 18:39sh
is actually bash or not, why do you want to usesh
when you know you use bash-specific functionality? – marcelm Oct 09 '21 at 09:06