0

I am trying to make a bash file that will rum my program N times and return the average time for the run with certain parameters. when I try with this code:

#!/bin/sh

NValues=(400)
kValues=(10)
echo "Enter Size(N)"
read N
CURR=0
i=1
sum=0
touch mean_times_log.txt
while [ $i -le $N ]
do      
        start=`date +%s`
        #running my program with arguments
        python3.8.5 -m invoke run -k=$1 -n=$2 --no-Random
        end=`date +%s`

        CURR=$((end - start))
        sum=$((CURR + sum))
        i=$((i + 1))


done
echo input k = $1 and n = $2 and Random = --Random.Execution mean time over N runs was $((sum/N)) seconds.>> mean_times_log.txt

mv mean_times_log.txt mean_times_log_$( date '+%Y-%m-%d_%H-%M-%S' ).txt
echo file was created

I get errors that I am not sure are connected. my program runs just fine by itself. the errors:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8.5/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.8.5/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/__main__.py", line 3, in <module>
    program.run()
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/program.py", line 375, in run
    self.parse_tasks()
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/program.py", line 739, in parse_tasks
    result = self.parser.parse_argv(self.core.unparsed)
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/parser.py", line 169, in parse_argv
    machine.handle(token)
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/parser.py", line 260, in handle
    self.see_value(token)
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/parser.py", line 409, in see_value
    self.flag.value = value
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/argument.py", line 120, in value
    self.set_value(arg, cast=True)
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/argument.py", line 151, in set_value
    self._value = func(value)
ValueError: invalid literal for int() with base 10: ''

 the  k =  and n =  and Random = --no-Random run ended with 1 seconds
Traceback (most recent call last):
  File "/usr/local/lib/python3.8.5/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.8.5/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/__main__.py", line 3, in <module>
    program.run()
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/program.py", line 375, in run
    self.parse_tasks()
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/program.py", line 739, in parse_tasks
    result = self.parser.parse_argv(self.core.unparsed)
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/parser.py", line 169, in parse_argv
    machine.handle(token)
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/parser.py", line 260, in handle
    self.see_value(token)
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/parser.py", line 409, in see_value
    self.flag.value = value
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/argument.py", line 120, in value
    self.set_value(arg, cast=True)
  File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/argument.py", line 151, in set_value
    self._value = func(value)
ValueError: invalid literal for int() with base 10: ''

would like to know what am I doing wrong or how can I get the results I need. thanks

egg33
  • 99
  • 5

1 Answers1

3

There are multiple issues:

  1. In bash // is not a comment. Use # instead.
  2. You pass the positional parameters $1 and $2 to your python program. However, it seems like you ran your script without any parameters, so these are empty and your python program complains about the empty string: invalid literal for int() with base 10: ''.
    It seems like you wanted to use $kValues and $NValues here (they are currently unused).
  3. kValues=(400) is an array with a single value. Are you sure you want an array?

In bash 5 or higher I would write it this way:

#! /usr/bin/env bash
k=10 n=400
read -p "Enter number of runs: " runs
t=$EPOCHSECONDS
for ((i=0; i<runs; ++i)); do
  python3 -m invoke run -k=$k -n=$n --no-Random
done
((t = EPOCHSECONDS - t))
echo "input k = $1 and n = $2 and Random = --Random.Execution" \
  "mean time over $runs runs was $((t/runs)) seconds." \
  > "mean_times_log_$(date +%Y-%m-%d_%H-%M-%S).txt"
echo "file was created"
Socowi
  • 25,550
  • 3
  • 32
  • 54
  • hi . I try to run it and always get 0 seconds – egg33 Apr 11 '21 at 12:07
  • Check your `bash --version`. This only works in bash 5 or higher. For older versions, replace `$EPOCHSECONDS` by `$(date +%s%3N)`. Also, I had a small bug in there. I used `t = t - EPOCHSECONDS` instead of `t = EPOCHSECONDS - t`, resulting in negative times. It is fixed now. – Socowi Apr 11 '21 at 12:08
  • Oh my bad.`$(date +%s%3N)` gives you milliseconds. I meant `$(date +%s)`. – Socowi Apr 11 '21 at 12:54