zl程序教程

您现在的位置是:首页 >  系统

当前栏目

go文件读写功能实现(io/ioutil)及实现shell脚本的调用(os/exec)

OSGo文件IOshell 实现 功能 脚本
2023-09-14 09:01:54 时间

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	)
	auth_content := fmt.Sprintf(`{
		"%v":{
			"username":"%v",
			"password":"%v",
			"insecure":%v
		},
		"%v":{
			"username":"%v",
			"password":"%v",
			"insecure":%v
		}
	}`, imgsync.SrcRepoUrl, imgsync.SrcUsername, imgsync.SrcPassword, *imgsync.Insecure, imgsync.DestRepoUrl, imgsync.DestUsername, imgsync.DestPassword, *imgsync.Insecure)
	ioutil.WriteFile("./bin/auth.json", []byte(auth_content), 0777)
	file, _ := os.Open("./bin/auth.json")
	defer file.Close()
	bytes_str, _ := ioutil.ReadAll(file)
	fmt.Println(string(bytes_str))

	os.Chdir("./bin")
	command := "./start.sh"
	cmd := exec.Command("/bin/bash", "-c", command)
	output, _ := cmd.Output()
	fmt.Println("---", string(output))