Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

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 parameter2 parameter3'

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

#!inash

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

./program <<< $inputstr

The input string ($inputstr) is the correct string ('parameter1 parameter2 parameter3'), 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?

question from:https://stackoverflow.com/questions/65939169/multiple-fortran-arguments-in-bash-script

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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$'
'$parameter2$'
'$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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...