1

I have a program I've written in c++ which outputs some simulation results to a .csv excel file.

According to some instructions I need to create a simple bash script that would run the .cpp file given the command "$ run_program" ($ is not a part of the command).

I've looked on Stackoverflow and other sites however I have not found a concrete answer to help me. I would also greatly appreciate it if those who answer can take some time to explain what the parameters mean.

Thank you.

How I should make a bash script to run a C++ program?

This is one of the links I've looked at, however I could not make heads or tails out of this.

8
  • 2
    Have you figured out how to compile the program? If so then just make the script contain the same line you use to compile the program, and on the second line of the script, put the program name Commented Mar 17, 2015 at 2:39
  • What have you tried? A bash script consists of commands that you'd normally run interactively in a bash shell. Presumably you've compiled your C++ code; what is the command you used to compile it? Commented Mar 17, 2015 at 2:39
  • 2
    The title says that you want to compile the program. The text says that you want to run the program. Those are two different actions. Which is it? Commented Mar 17, 2015 at 2:39
  • It should do both. For example I can create a Makefile that can both compile and execute my code, or I can create a Makefile where I simply compile the .cpp program and then type "./program_name" to run it. However using a Makefile I need to use the command "make program_name" however I just want to use "run_program". Commented Mar 17, 2015 at 3:01
  • 3
    1) Look up something like bash hello world. 2) Lookup bash arguments. 3) Lookup how to compile and run a c++ program linux. 4) Put them togeather Commented Mar 17, 2015 at 3:17

2 Answers 2

8

i dont know the command you are using to compile your c++ program but this might help you.

  1. Create a file with ".sh" extension and open it with your favorite text editor.
  2. Paste this code (change compiling line with line you are using to compile your progam)

    #!/bin/bash
    #Run this in terminal
    #+ Command to compile c++ program. here i used common one
    g++ filename.cpp -o anyname
    exit 0
    
  3. Now you need to run this script, To do this open a terminal

    chmod u+x scriptname.sh

Then run the script by ./scriptname.sh Hopefully this will compile your program.

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

2 Comments

This is close to what I did but the requirements explicitly state the program must be compiled using "run_ABP". Hence I cannot use the "./"
I have a make file, then another file called "run_ABP". In the make file I have a compile command for the file ABP.cpp called "ABP". In the file called "run_ABP" I use "#!/bin/sh <new line> make ABP_NAK;". Using the command "chmod u+x run_ABP" I am able to compile using the command "./run_ABP"
0

It sounds like a Makefile is what you are looking for here. Definitely worth getting a handle on if you deal with programming.

Comments

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.