0

I only recently started programming. I have a shell script which contains a c-program. At the end of the script, the c-program is ran using the commands

gcc stadist.c -lm 
a.out < XXXXX | sort -n -k3 > YYYYY 
rm a.out 
rm stadist.c 
rm XXXXX 

Running this script gives a.out: not found. The file YYYYY is created but it is empty. How can i solve this problem. I am on Linux ubuntu 16.04 LTS.

7
  • So you're saying that your program doesn't compile? Commented Jul 6, 2017 at 8:21
  • 2
    I have a shell script which contains a c-program....ummmm you might need to rephrase that. Commented Jul 6, 2017 at 8:21
  • 2
    Why is the script removing the source? Commented Jul 6, 2017 at 8:22
  • 2
    As for your problem, try ./a.out Commented Jul 6, 2017 at 8:23
  • is . on your path - try ./a.out - but I hope you have a backup of that source file.... Commented Jul 6, 2017 at 8:23

2 Answers 2

2

The shell cannot find a.out because it isn't looking in the current directory. That is best practice. (ie, do not add . to PATH). Just do:

gcc stadist.c -lm &&
./a.out < XXXXX | sort -n -k3 > YYYYY 
rm a.out 
rm stadist.c 
rm XXXXX 

Note that I added && after the invocation of gcc so that the script does not attempt to run a.out if the compilation fails. You'll probably want to add more robust error checking.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow!!! This is magical. I don't understand fully the implications of the changes you made but it worked. The required output was generated although running another script that calls this gave some error messages that have to do with some installed software. That i can take care of. Thanks
All the && does is short circuit the command if the first fails. It is (almost) equivalent to if gcc stadist.c -lm ; then ./a.out ...; fi As for how the shell searches for binaries; IMO that's pretty important to understand, but suffice to say that by default it does not look in the current directory, so you need to explicitly tell it to by referencing the command with a relative path.
0

It seems that you do not fully grasp what your shellscript does. If you are getting "./a.out: not found" when you run the script it means that the program did not compile correctly. This is probably due to the line:

rm stadist.c

where you actually delete your sourcefile, meaning the script will work at most once. You should check to see that you still have stadist.c somewhere and try to compile and run it outside of the script.

2 Comments

I grasp what the shellscript is set to do but unfortunately not how it does it. i am less than 3months old in this. The stadist.c isnt a file of its own. It is concatenated to the shellscript. Within the script after some lines there is this: cat > stadist.c << EOF followed by other lines. The command: rm stadist.c may not be the problem as the main shellscript is intact. It cant run outside the script also. It needs the script for input and the script needs its output. This is what i understand. My knowledge is still limited in this subject though. thanks
Well, if there is more to the script than what you have provided maybe you should show the entire script? And is stadist.c a program you've written yourself or is it just some random program? I think you need to provide a little more information, so i suggest you edit your original question wwith more info.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.