Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I try to make this very simple bash program that must make the following operations:

  1. modify a file java n times: every time I must change a single word in a single line of the file (the line is 11) I know there is the following command that replace a single word in a particular line of a program wih another one:

    sed -i "11s/1/$n/" IC_masslessPlanetesimal.java
    

    if I do the above command I can modify only one time the file.java because in the second iteration of the for cycle I have not the same word as in the first iteration: I start with 1 in line 11, than in the fist iteration I change with $n (that is equal to 2), but now HOW I can modify the sed command to change again the word (that is now equal to 2) to have, for example, 3 in the 11th line?

  2. compile the file java: it's ok;

  3. modify a file.txt: again the same problem as in 1) I want to change a word in a line iteratively with a for cycle or something like that;

  4. pass it to an executable file: this is the second problem. I have modifIed the param.txt file and now I want to read it into an executable file called exec. If I do this from the terminal I must launch the exec. program with the command ./exec and after that I must write from screen the file.txt name. Now is it possible to launch a program from bash and read with it the file.txt? I try with:

    sed -i "13s/Zero/$n/" param.txt
    ./exec < param.txt   
    

that script must change the line 13 from Zero to $n in param.txt how I can launch the exec program with the command ./exec with the specific param.txt file.

#!/bin/bash

n=0
for n in "2" "3" 
do
  sed -i "11s/1/$n/" IC_masslessPlanetesimal.java
  javac IC_masslessPlanetesimal.java  
  java IC_masslessPlanetesimal
 sed -i "13s/Zero/$n/" param.txt
 ./exec < param.txt   
done

thanks a lot !!!

share|improve this question
    
Why not start with template files instead of banging up the existing ones? – Ignacio Vazquez-Abrams Jan 4 '14 at 4:51
2  
why don't you just change your java program to accept ARGUMENTS instead! Then you don't have to do this hack! In your script, just write out n to a file and the next time you run, you cat the file into a variable, add 1 to the value and write it back to the file. Then run the java program passing in an argument. – Peon Jan 4 '14 at 6:21
    
What you're doing (though needlessly complicated) should work fine. What is the actual problem you have? – terdon Jan 4 '14 at 14:01
  1. Instead of editing the file in place:

    sed -i "11s/1/$n/" IC_masslessPlanetesimal.java
    

    copy it into a template file, say IC_masslessPlanetesimal.java.template, then create a new version of it for each n:

    sed "11s/1/$n/" < IC_masslessPlanetesimal.java.template > IC_masslessPlanetesimal.java
    
  2. Same as 1

  3. Yes, ./exec < param.txt is correct. Doesn't that work?

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .