1

I'm pretty new to bash shell programming, but I want to learn. I have a C++ file called run.cpp when run it gives lots of text files results such as output1.txt, output2.txt, etc until output2000.txt. First I want to create a file named Output; then compile and run the program. After I get all the txt file results, I want to move them inside Output file with the * wildcard.

I can do this from the shell seperately. However, I was not able to write a script for this that does it automatically.

Any help, or any point to a good reference on shell scripting would help a lot at this point.

Thank you in advance.

My shell commands are:

    mkdir Output
    g++ run.cpp -o run
    cp *.txt /Output
3
  • "Questions" that are merely requirements without showing any effort are subject to removal. They were once considered off-topic, but that's still being debated. Many people consider it to be rude, so please try to follow the guidelines. Thanks. Commented Sep 23, 2014 at 16:52
  • 1
    you probably want to learn about GNU Make, the most-used industry standard way to compile C and C++ projects. if you are using Linux, your distro probably already ships with make tools. Commented Sep 23, 2014 at 16:53
  • 1
    Note that if you showed us the sequence of commands you type at the shell, we would see the shell script immediately -- you would have self-answered your question. That's the advantage of shell scripts. Commented Sep 23, 2014 at 16:53

1 Answer 1

1

Write a script with this content. Let's name the script script.sh

#!/bin/sh

mkdir Output
g++ -o run run.cpp
./run
mv output*.txt Output

Run the script. Run the script with whatever your shell is. If it's bash,

bash script.sh

Note that if your script is in another directory and you run from that directory, you'll have to specify the exact location of your .cpp file, where you want the executable to go, the exact path of the executable when running, the path of the output files from the program and where you want to create your Output directory. Also I'm assuming compilation is done with g++.

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

2 Comments

Rather than move the files after you run the program, why not just have the program create them in the right place to begin with (i.e. mkdir Output; g++ ... ; cd Output; ../run)?
@twalberg. Clearly there are different ways to achieve the same result.

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.