如何包装交互式命令 [英] How wrap a command that is interactive

查看:72
本文介绍了如何包装交互式命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个ftp包装器,在生成之前它会做一些事情,我可以很容易地在shell脚本中完成它,但是想知道如何在运行中完成它

I am building an ftp wrapper that does some stuff before I spawn, I could easily do it in a shell script but wondering how I could do it in go

exec.Command适用于简单命令.

While exec.Command works for simple commands.

out, err := exec.Command("ls").Output() // Works

如何包装交互式命令,例如ftp

How do I wrap commands that are interactive e.g., ftp

out, err := exec.Command("ftp").Output()

它刚刚退出.我该如何处理stdin?

It just exits. How do I deal with stdin ?

例如bash等效项:

> ./t.sh 
Welcome to myftp 

ftp> open blahblah.com

> cat t.sh 
#!/bin/bash
echo "Welcome to myftp "
#extra commands such as auth/authoriz.. etc.,
shift
echo "$@"
ftp

相当于c ++:

int main() {
    system("ftp");
    return 0;
}

推荐答案

我可能会做这样的事情,它更本地化并且不涉及外部程序包

I would probably do something like this which is more native and doesn't involve an external package

package main
import (
    "os"
    "os/exec"
)

func main() {

    cmd := exec.Command("ls")
    // redirect the output to terminal
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr

    cmd.Run()

}

这篇关于如何包装交互式命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆