0

I need to run a Fortran(90) program with different parameters. The Fortran program in interactive, and receives several input parameters sequentially (it asks for each parameter individually, i.e. they are read in at different instances).

My idea is to write a short bash script to change the pertinent parameters and do the job automatically. From the command line the following works fine:

./program <<< $'parameter1\nparameter2\nparameter3'

When trying to change the parameters in a bash script, things stop working. My script is as follows:

#!\bin\bash

parameter1=1
parameter2=2
parameter3=3
inputstr=$parameter1'\n'$parameter2'\n'$parameter3

./program <<< $inputstr

The input string ($inputstr) is the correct string ('parameter1\nparameter2\nparameter3'), but is interpreted as a whole by bash, i.e. it is not given as three independent parameters to the Fortran program (the whole string is interpreted as parameter1).

I have tried several ways of putting inputstring within brackets, apostrophes or other, but none seems to work.

Is there a way to make this work in a automated manner?

1 Answer 1

1

You still have to use $'...' quoting to embed literal newlines in the value of inputstr.

#!/bin/bash

parameter1=1
parameter2=2
parameter3=3
inputstr=$parameter1$'\n'$parameter2$'\n'$parameter3

./program <<< $inputstr

You don't actually need to use $'...' quoting, though.

#!/bin/bash

parameter1=1
parameter2=2
parameter3=3
inputstr="$parameter1
$parameter2
$parameter3"

./program <<< $inputstr

or just

#!/bin/bash

parameter1=1
parameter2=2
parameter3=3


./program <<EOF
$parameter1
$parameter2
$parameter3
EOF

Here strings are nice for repeated, interactive use; they are less necessary when you are using a decent editor to write a script once.

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

1 Comment

Excellent! Thank you very much for the prompt answer! Saved my day :)

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.