Monday, April 11, 2022

[SOLVED] Why does my shell script make my C++ program output 0?

Issue

I have a program that is compiled correctly and it did run properly. I use it to calculate something, and it returns the time it took. If I run it in the terminal, it just puts the correct result in the file:

# use method1.txt to calculate a mastrix 
# whose length of the side is 50, 
# and put the result into result1.txt
> ./method1.exe 50 result1.txt
> cat result1.txt
5000    0.279

And I will get the correct answer, which means a matrix with sides 5000 long is calculated, and cost time is 0.279 s.


However, when I run this program several times with shell sript and change the edge length of the matrix, the outputs are all 0.

Here is my shell script file:

# create txt file
TIME="$(date +%H_%M_%S)"
filename="result1_$TIME.txt"
touch $filename

# loop the program, till num reaches 100000
for ((i=100; i <= 10000; i = i + 100))
do
    ./method1.exe i $filename
    echo "loop finished for $i"
done;

And the result is like:

amount\ttime
0   0
0   0
0   0

They are just all 0s, could anybody tell me why?


Solution

In ./method1.exe i $filename you forgot the dollar for the i: should be $i.



Answered By - Victor Eijkhout
Answer Checked By - Robin (WPSolving Admin)