1

I am new to bash and trying to run the following command in bash.

name="Na"
for j in {1..3}
do
mkdir $j
cd $j
 ../../../../../abcinp "$name$j" 1 LennardJones 5.0 30 300 5 30 $j Na

cd ../
done

echo All done

After running this script I need to enter the following parameters

Parameters for atom 0: sigma epsilon >1.24, 1.0

It asks me as many times the loop runs and I need it. Is there a way I can pass these parameters in the script and the script reads it from there? I have not found any clue on this. Any help would be really appreciated.

##updates following meisters suggestion I did the following

#!/bin/bash
./simcode.sh << EOF
1.24 1
1.24 1
1.24 1

When I run the loop three times the parameter 1.24 1 is used for first run only. How can I redirect it for other runs? simcode.sh contains the original script mentioned above.

3
  • I think you need to look at the source of abcinp - that is giving the error. See what route through its code leads to that error message and what parameters it requires and in what format. Good luck - I think you would need quotes around LennardJones and Na though in that call. Commented Jul 15, 2021 at 15:19
  • The script is working and I need to enter those parameters that is asking. I just want to know the way the script reads these parameters itself from a file or any means. Commented Jul 15, 2021 at 15:28
  • Why not wrap ./simcode.sh in a hoop itself? That way you would just call ./simcode.sh 1.24 1 each time it was needed. For a specific answer, you need to describe more fully how running script 1 results in ./simcode.sh being called X-number of times. That way, an answer can show you how to tie both scripts together. Commented Jul 15, 2021 at 18:51

1 Answer 1

1

From what I understand, the program is asking you to type those parameters, because it won't read them as command line arguments. If that is the case, you could try to read from a file (but there's a better option!) like this in your script.sh:

#!/bin/bash
simulationProgram < fileWithArguments.txt

The file "fileWithArguments.txt" should have every argument the program is asking you to type, one per line and in the order the program asks them.

About the better way? Use a heredoc, it's like putting a file inside a file:

#!/bin/bash
simulationProgram << EOF
argument1
...
lastArgument
EOF

anotherCommands # If you need them

The EOF tells bash where the here-doc ends, you can use any word, but be sure that the one after << and at the end of the arguments are the same. You can also use bash variables inside the here-doc.

EDIT: I wasn't as clear as I should have been. The heredoc should go in the script with the loop, not in the bash call. Here is what your script should look like, assuming it only asks for 1.24 and 1 each time you run the program (note that 1.24 and 1 are in different lines):

#!/bin/bash
name="Na"
for j in {1..3}
do
  mkdir $j
  cd $j
  ../../../../../abcinp "$name$j" 1 LennardJones 5.0 30 300 5 30 $j Na << EOF
1.24
1
EOF

  cd ../
done

echo "All done"

What happens inside:

  1. The name variable gets initialized.
  2. The loop starts, j is 1.
  3. You create the dir $j (so it will be called "1")
  4. You go to that directory.
  5. You launch abcinp, which I assume calculates a molecular parameter or something (this physics program all have the annoying habit of asking for manual input), with a series of arguments.
  6. abcinp launchs, and asks for manual input.
  7. Bash answers the manual input with the first line after "EOF": 1.24.
  8. Bash does the same the second time asks for manual input, with the second line (so it will answer 1).
  9. Then the program stops asking for parameters and starts calculating. When it finish, you move to the parent directory and start with the next iteration of the loop. After all of them are done, echo will print "All done".

Note: I'm assuming abcinp asks for a parameter, then you press enter, and then asks for another one. If you literally have to type "1.24 1" in the same line, they should be in the same line in your script.

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

6 Comments

Thank you so much. I tried the first option and the parameters are taken for the first run only. I want to run the loop up to 30 and take the parameters 30 times. Any suggestion?
I did not understand this "but be sure that the one after << and at the end of the arguments are the same."
It sounds like the path for the file is only right for the first iteration. Try a more easily accesible path, like "~/fileWithArguments.txt". If it works the first time, it means it means you almost have it. Regarding the EOF part, it means that if you write "<< EOF", after your arguments (where a real file would end) you must write EOF. If you write "<< ANYWORD" (it can only be one word, dont use spaces), after the arguments you should write ANYWORD.
I want to pass 1.24, 1.0 four times. Now, I am able to run only once. Could you please edit your second answer type with ANYWORD, how it looks?
Both of your suggestions are working, but I need them as many times as loop runs. Now, it only gives for first iteration in the loop.
|

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.