0

I have an executable file (Something.exe) that takes in two inputs when I run it. For example it does something like this:

    My-MacBook:Folder my$ ./Something.exe
    Enter first input: someimage.tif
    Enter second input: x y z coordinates 
    123 456 23.00000 24.0000 59.345

I run the program and enter the two inputs separately when prompted and then the program gives results.

However, how can I enter the whole process into a single line, meaning:

    My-MacBook:Folder my$ ./Something.exe someimage.tif x y z coordinates
    123 456 23.00000 24.0000 59.345

How do I do this in a single line on the terminal so I don't have to type in the inputs when prompted? Is there something that I need to tweak in the programs code? The program is written in Fortran 90.

3
  • 1
    Look for "getting arguments" if you want to tweak the program... genomeek.wordpress.com/2012/02/09/… Commented Jan 26, 2017 at 22:09
  • @ Mark Setchell Thank you! I will check into this. Commented Jan 26, 2017 at 22:22
  • Do you mean you want to run the program lots of times with the same image but different coordinates? Maybe you could show the first 3 commands you would run if you got the answer you wanted... Commented Jan 26, 2017 at 22:26

3 Answers 3

1

If the program is just reading from stdin, you can simply do

printf '%s\n%s\n' 'someimage.tif' 'x y z coordinates' | ./Something.exe

Or if the shell you're using is bash:

echo $'someimage.tif\nx y z coordinates' | ./Something.exe
Sign up to request clarification or add additional context in comments.

2 Comments

Hello! The printf worked. Thank you!! If it's not too much trouble, would you mind please explaining what the '%s\n%s\n'?
@Guest See man 1 printf for the command and man 3 printf for the format string. Basically, the first argument is a string that describes how to format the output. %s means "output the next argument here (as a string)"; \n means "output a newline (line break)".
0

One classic way of pushing commandline arguments into an interactive commandline program is to use an expect script. For your example exe, here is an expect script that should work:

#!/usr/bin/env expect
set tif [lindex $argv 0]
set x [lindex $argv 1] 
set y [lindex $argv 2]
set z [lindex $argv 3]
set coords "$x $y $z"
spawn ./Something.exe
match_max 100000
expect "first input:"
send -- $tif
send -- "\r"
expect "second input:"
send -- $coords
send -- "\r"
expect eof

Write this to a file, say, automate.exp, make it executable, then run it like:

./automate.exp someimage.tif xcoord ycoord zcoord

Comments

0

IMHO, the easiest way to do this is to "put a shell wrapper" around Something.exe. Let's say we want the new command to be GoBaby, we would save the following as GoBaby:

#!/bin/bash
################################################################################
# GoBaby
# Wrapper around Something.exe, to be used as:
#
# ./GoBaby image.tif "x y z"
################################################################################
# Pick up the two parameters we were called with
image=$1
xyz=$2
# Send the parameters into Something.exe
{ echo "$image"; echo "$xyz"; } | ./Something.exe

Then, you make the wrapper script executable (just necessary a single time):

chmod +x GoBaby

Now you can just run:

./GoBaby image.tif "x y z"

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.