在Unix/Linux中,如何在终端上使用输入运行.EXE?

在Unix/Linux中,如何在终端上使用输入运行.EXE?,linux,shell,unix,terminal,Linux,Shell,Unix,Terminal,我有一个可执行文件(Something.exe),它在运行时接受两个输入。 例如,它可以执行以下操作: 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 我运行程序,在提示时分别输入两个输入,然后程序给出结果 但是,如何将整个流程输

我有一个可执行文件(Something.exe),它在运行时接受两个输入。 例如,它可以执行以下操作:

    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
我运行程序,在提示时分别输入两个输入,然后程序给出结果

但是,如何将整个流程输入到一行中,即:

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

如何在终端上的单行中执行此操作,以便在提示时不必键入输入?在程序代码中有什么我需要调整的吗?该程序是用Fortran 90编写的

如果程序只是从stdin读取数据,您只需执行以下操作

printf '%s\n%s\n' 'someimage.tif' 'x y z coordinates' | ./Something.exe
或者如果您使用的shell是bash:

echo $'someimage.tif\nx y z coordinates' | ./Something.exe

将命令行参数推入交互式命令行程序的一种经典方法是使用expect脚本。对于您的exe示例,以下是一个expect脚本:

#!/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
将其写入文件,例如automation.exp,使其可执行,然后像以下方式运行:

./automate.exp someimage.tif xcoord ycoord zcoord

依我看,最简单的方法是在
Something.exe
周围“放置一个外壳包装器”。假设我们希望新命令为
GoBaby
,我们将以下内容保存为
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
然后,使包装器脚本可执行(只需执行一次):

现在,您可以运行:

./GoBaby image.tif "x y z"

如果要调整程序,请查找“获取参数…”马克·塞切尔谢谢你!我会检查一下。你是说你想用相同的图像但不同的坐标运行程序很多次吗?如果你得到了想要的答案,也许你可以展示你将运行的前3个命令……你好!printf成功了。非常感谢。如果不太麻烦的话,请您解释一下'%s\n%s\n'?@Guest在命令和格式字符串中看到了什么。基本上,第一个参数是描述如何格式化输出的字符串<代码>%s表示“在此处输出下一个参数(作为字符串)”<代码>\n表示“输出换行符(换行)”。
./GoBaby image.tif "x y z"