Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I wanted to compile my program and I wanted to know how many times I have compiled it.

How do I make a .sh file to do that? (I'm using mac, is it .sh file?)

share|improve this question

closed as off topic by Walter, vartec, gnat, Bernard, Karl Bielefeldt Sep 28 '12 at 17:39

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

Good rule of thumb... Implementation questions belong on SO, white board questions belong here. I think this would be more appropriate on SO. I've voted to migrate it. Please do not cross post your question there. –  Walter Sep 28 '12 at 12:27

5 Answers 5

It seems odd to be using a .sh script for build automation. There are several other tools (make, ant, maven) which are much better suited to doing builds and tracking build numbers.

Using one of the more industry standard tools for build automation will, in the long run, make for something that is easier to maintain both for yourself and others.

share|improve this answer
+1 Maven already comes with variables and pom.xml configs that do just that, it's as easy as adding an extra line to your pom file. –  Shivan Dragon Sep 28 '12 at 13:30
@ShivanDragon ghads, thank you for catching that. Looking at the page I had quickly linked, that was for maven 1 rather than a more modern version. –  MichaelT Sep 28 '12 at 13:52

The following script will do just that in a file declared in the "file" variable:

#! /bin/bash


if ! test -f $file; then echo 0 > $file; fi
echo $(($(cat $file) + 1)) > $file

At the end of it you can add the command to invoke the build for your project.

share|improve this answer

Store in a file how many times you have compiled.
When you start the script read the value from the file, increment it and write it back. If the file not exists just create it and write 1 in it.

share|improve this answer

Its quite Simple, Make a counter in the begining of your script and save the last value in a text file. For example :

Read the value first from the file, like counter =cat /tmp/SomeDIR/LOGS.txt rm /tmp/SomeDIR/LOGS.txt

counter=expr $counter + 1 # increament the counter

Store this counter value in a file

echo $counter > /tmp/SomeDIR/LOGS.txt

share|improve this answer

If you want to store a state variable that remembers how many times an action took place, the best way to do it would be in an external file that is separate from the script file. So you could read and write to this file, every time you ran the program. The basic pseudocode would be as follows:

  1. Get External file name via Command line argument/other means
  2. Read compile count value
  3. If no value exists
    • Write 1 to external file
  4. Else
    • Increment Count Value
    • Write value to external file
  5. Exit
share|improve this answer

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