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

时间:2017-01-26 21:59:31

标签: 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

我运行程序并在提示时单独输入两个输入,然后程序给出结果。

但是,如何将整个过程输入一行,这意味着:

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

如何在终端上的一行中执行此操作,以便在出现提示时不必输入输入?我需要在程序代码中调整一些东西吗?该程序是用Fortran 90编写的。

3 个答案:

答案 0 :(得分:0)

如果程序只是从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

答案 1 :(得分:0)

将命令行参数推送到交互式命令行程序的一种经典方法是使用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

将此文件写入文件,例如,automate.exp,使其可执行,然后运行它:

./automate.exp someimage.tif xcoord ycoord zcoord

答案 2 :(得分:0)

恕我直言,最简单的方法是"在Something.exe附近放置一个shell包装器" 。我们假设我们希望新命令为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

然后,使包装器脚本可执行(只需要一次):

chmod +x GoBaby

现在你可以运行:

./GoBaby image.tif "x y z"