WJahn
WJahn

Reputation: 113

Multiple Fortran arguments in bash script

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?

Upvotes: 0

Views: 233

Answers (1)

chepner
chepner

Reputation: 531245

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.

Upvotes: 1

Related Questions