Search code examples
bashbash-completion

Is there a way to pass the parameters within the script in bash?


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.


Solution

  • 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.