Question

My $SHELL is tcsh. I want to run a c shell script that will call a program many times with some arguments changed each time. The program I need to call is in Fortran. I do not want to edit it. The program only takes arguments once it is executed, but not on the command line. Upon calling the program in the script, the program takes control (this is where I am stuck currently, I can never get out because the script will not execute anything until after the program process stops); at this point I need to pass it some variables, then after several iterations I will need to ctrl-c out of the program and continue with the script.

How can this be done?

Was it helpful?

Solution

To add to what @Toybuilder said, you can use a "here document". I.e. your script could have

./myfortranprogram << EOF
first line of input
second line of input
EOF

Everything between the "<<EOF" and the "EOF" will be fed to the program's standard input (does Fortran still use "read (5,*)" to read from standard input?)

And because I think @ephemient's comment deserves to be in the answer:

Some more tips: <<'EOF' prevents interpolation in the here-doc body; <<-EOF removes all leading tabs (so you can indent the here-doc to match its surroundings), and EOF can be replaced by any token. An empty token (<<"") indicates a here-doc that stops at the first empty line.

I'm not sure how portable those ones are, or if they're just tcsh extensions - I've only used the <<EOF type "here document" myself.

OTHER TIPS

What you want to use is Expect.

Uhm, can you feed your Fortran code with a redirection? You can create a temporary file with your inputs, and then pipe it in with the stdin redirect (<).

Not a tcsh user, but if the program runs then reads in commands via stdin then you can use shell redirection < to feed it the required commands. If you run it in the background with & you will not block when it is executed. Then you can sleep for a bit, then use whatever tools you have (ps, grep, awk, etc) to discover the program's PID, then use kill to send it SIGTERM which is the same as doing a ctrl-c.

This is a job for the unix program expect, which can nicely and easily interactively command programs and respond to their prompts.

I was sent here after being told my question was close to being a duplicate of this one.

FWIW, I had a similar problem with a csh C shell script.

This bit of code was allowing the custom_command to execute without getting ANY input arguments:

foreach f ($forecastTimes)
    custom_command << EOF
        arg1=x$f;2
        arg2=ya
        arg3=z,z$f
        run
        exit
        EOF
end

It didn't work the first time I tried it, but after I backspaced out all of the white space in that section of the code I removed the space between the "<<" and the "EOF". I also backspaced the closing "EOF" all the way to the left margin. After that it worked:

foreach f ($forecastTimes)
    custom_command <<EOF
        arg1=x$f;2
        arg2=ya
        arg3=z,z$f
        run
        exit
EOF
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top