从Go程序内部调用源代码

时间:2015-06-04 22:16:05

标签: go

为了获得乐趣并更好地学习Go,我试图在Go中重新实施抗原。

问题是:source是一个shell内置函数,因此我无法使用os/exec Command函数调用它,因为它需要{{1}中的可执行文件}}

我该怎么做?并且,是否可以从go程序中使PATH影响用户shell?

1 个答案:

答案 0 :(得分:0)

您可以直接在终端设备中编写命令。但是,要做到这一点,首先您需要知道哪个设备正在使用该用户。执行程序的脚本可以是一个解决方案。

#!/bin/bash
echo Running from foo script, pid = $$
go run foo.go `tty`

然后,程序必须将命令写入终端设备。

package main

import (
    "C"
    "fmt"
    "os"
    "syscall"
    "unsafe"
)

func main() {
    // Get tty path
    if len(os.Args) < 2 {
        fmt.Printf("no tty path\n")
        os.Exit(1)
    }
    ttyPath := os.Args[1]

    // Open tty
    tty, err := os.Open(ttyPath)
    if err != nil {
        fmt.Printf("error opening tty: %s\n", err.Error())
        os.Exit(2)
    }
    defer tty.Close()

    // Write a command
    cmd := "echo Hello from go, pid = $$\n"
    cmdstr := C.CString(cmd)
    cmdaddr := uintptr(unsafe.Pointer(cmdstr))
    for i := range []byte(cmd) {
        _, _, err := syscall.Syscall(syscall.SYS_IOCTL, tty.Fd(), syscall.TIOCSTI, cmdaddr+uintptr(i))
        if uintptr(err) != 0 {
            fmt.Printf("syscall error: %s\n", err.Error())
            os.Exit(3)
        }
    }
}

以下是输出示例:

$ echo $$
70318
$ ./foo 
Running from foo script, pid = 83035
echo Hello from go, pid = $$
$ echo Hello from go, pid = $$
Hello from go, pid = 70318

请注意,我正在使用./而不是source执行脚本,因此脚本的PID不同。但是后来,go程序执行的命令具有相同的PID。

相关问题