在golang中,可以使用os/exec包的Command函数来执行shell命令,并将参数传递给shell脚本。

例如,下面的程序将执行一个名为test.sh的shell脚本,并将两个参数传递给它:

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    arg1 := "hello"
    arg2 := "world"
    cmd := exec.Command("./test.sh", arg1, arg2)
    output, err := cmd.CombinedOutput()
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(string(output))
}

在test.sh中,可以通过$1和$2来获取这些参数:

#!/bin/bash

echo "The first argument is: $1"
echo "The second argument is: $2"

这将输出:

The first argument is: hello
The second argument is: world
``
sh文件如何接收来自golang的参数

原文地址: https://www.cveoy.top/t/topic/fIut 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录