1

A fortran program requires three inputs to be passed interactively:

First input is just letter "D", second input is a file "A.cpt", and third input is a name for the output file that can be for example "B.out".

I have been trying to use echo command, but it is not working:

echo "D\nA.cpt\nB.out" | ./fortran_program 

Any ideas?

UPDATE:

When using EOF, as suggested below, the program is going to an infinite loop repeating the following lines:

Please try again!
Please enter input binary hydra/quanta plot (old) filename
defaults <dotsurface_0_165.qpt> ext:<.qpt> (abort by EXIT or ^D) :  Sorry unable to open file: D.qpt
               or file: D
Please try again!
Please enter input binary hydra/quanta plot (old) filename
defaults <dotsurface_0_165.qpt> ext:<.qpt> (abort by EXIT or ^D) :  Sorry unable to open file: D.qpt

The source code for this program is on Github if that helps:

https://github.com/osmart/hole2/blob/master/src/qpt_conv.f

2
  • Would the intrinsic function get_command_argument work for your needs, or is it necessary to use (bash)? Commented Jul 16, 2019 at 4:57
  • When typing D, the program then expects a filename as the next argument. input, for instance, for the file input.qpt. Your second argument being A, the program looks for the file A.qpt. Commented Jul 16, 2019 at 8:47

2 Answers 2

4

If you are using bash (or similar), I think echo -e or printf will be useful (but please check this page also). For example, either of

echo -e "D\nA.cpt\nB.out" | ./a.out
printf "D\nA.cpt\nB.out" | ./a.out

gives

str(1) = D         
str(2) = A.cpt     
str(3) = B.out

(for a.out generated from test.f90 below). Another approach may be to use "here document", e.g.

./a.out <<EOF
D
A.cpt
B.out
EOF

which I often use in a bash script (because the input part becomes more readable and looks like a separate input file).

!! test.f90
program main
    implicit none
    character(10) :: str( 3 )

    read *, str( 1 )
    read *, str( 2 )
    read *, str( 3 )

    print *, "str(1) = ", str(1)
    print *, "str(2) = ", str(2)
    print *, "str(3) = ", str(3)
end
Sign up to request clarification or add additional context in comments.

Comments

0

Alternatively, you can use actual line breaks in the echo command:

user@host$ echo "1                             
2
3" | ./test_echo 
           1           2           3
user@host$ 

When you type "enter" after echo "1, you can keep on entering the quote-delimited string.

Comments

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.