0

I have simple bash script

#!/bin/bash

make
./code1_test/test_syn1
./code2_test/test_syn2
./code3_test/test_syn3

test_syn are my program tests. In every of these programs, I'm using fopen command to open .txt file. If I run programs from command line, I have a correct test output, but when I'm using this bash script, fopen return me NULL, so I can't open my .txt files and I can't run tests. Why is this happening, and how can I fix this?

4
  • Can you create a minimal test_syn file that reproduces this? Nov 22, 2017 at 0:18
  • 3
    It's probably a path issue. Also be good to post the code of test_syn.
    – sashang
    Nov 22, 2017 at 0:19
  • 3
    Chances are you're using a relative path in your program, so when you run the script the programs are looking in the directory the script was run from.
    – hnefatl
    Nov 22, 2017 at 0:19
  • 3
    Whenever I see a question like "<some basic feature> of <some programming language> isn't working", I can guarantee two things: the poster is a beginner, and <feature> is working just fine. Do some basic debugging: print out the return and errno values of your calls, try to find the full pathname of the files you're opening, etc. It's almost certainly an issue of current directory/path. Nov 22, 2017 at 0:22

1 Answer 1

2

If you’re using fopen() with a relative path, you should execute the script from its directory. Try this:

#!/bin/bash
make
(cd code1_test/ && exec ./test_syn1)
(cd code2_test/ && exec ./test_syn2)
(cd code3_test/ && exec ./test_syn3)

Otherwise your program tries to open your .txt in the directory from where your run your Bash script, that’s not what you want.

If you want to be able to run your program from anywhere, you might try not to use a relative path when you fopen().

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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