如何执行由我的程序创建的文件?

huangapple go评论72阅读模式
英文:

How to execute file created by my program?

问题

我正在尝试执行我程序刚刚创建的*.ics*文件。基本上,我的程序是一个简单的命令行日历应用程序,它生成.ics文件。如果我的程序能够执行这个文件并将其直接添加到操作系统的日历应用程序中,而不需要通过操作系统图形界面进行不必要的搜索和执行,那就太好了。

我在这里粘贴了主要的函数以便更好地理解。

func main() {

	serialized, name := cal()

	f, err := os.Create(name + ".ics")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	_, err2 := f.WriteString(serialized)
	if err2 != nil {
		log.Fatal(err2)
	}
	cmd := exec.Command(name + ".ics")
	err = cmd.Run()
	if err != nil {
		log.Fatal(err)
	}
}

正如所示,我尝试使用exec.Command,但它不起作用。我甚至尝试使用额外的前缀,如./~/,但也没有起作用。

错误信息:

fork/exec ./Meeting.ics: permission denied

exec: "Meeting.ics": executable file not found in $PATH

总结一下 - 我想跳过用户必须找到文件并打开它的过程。我希望将其自动作为我的应用程序的一部分。

这是我的存储库,如果有帮助的话:https://github.com/lenoopaleno/golang-calendar

我正在使用WSL 2,Ubuntu 22.04。

英文:

I'm trying to execute .ics file that my program just created. Basically, my program is simple CLI calendar app, which generates .ics file. It would be nice if my program would execute this file and add it straight to OS calendar app, without unnecessary searching and executing through OS GUI.
I paste main function to better understanding.

func main() {

	serialized, name := cal()

	f, err := os.Create(name + ".ics")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	_, err2 := f.WriteString(serialized)
	if err2 != nil {
		log.Fatal(err2)
	}
	cmd := exec.Command(name + ".ics")
	err = cmd.Run()
	if err != nil {
		log.Fatal(err)
	}
}

As it's shown I tried with exec.Command, but it doesnt' work. I was even trying with additional prefixes like ./ or ~/, but it as well didn't work.

Error messages:

fork/exec ./Meeting.ics: permission denied

exec: "Meeting.ics": executable file not found in $PATH

So to sum it up - I want to skip the process where the user has to find a file and open it. I want to make it automatically as a part of my application.

Here's my repository if it would help https://github.com/lenoopaleno/golang-calendar

I'm working on WSL 2, Ubuntu 22.04

答案1

得分: 1

除了上面的评论之外,你的代码中可能存在一个问题,即 defer f.Close()

defer 语句会在函数结束时执行。在此之前,你的文件可能已经关闭,也可能还可以被其他进程访问。
其次,在类Unix操作系统下,你很可能需要为要运行的程序设置执行权限。

程序调整如下:

func main() {

    serialized, name := cal()

    f, err := os.Create(name + ".ics")
    if err != nil {
        log.Fatal(err)
    }

    _, err2 := f.WriteString(serialized)
    if err2 != nil {
        log.Fatal(err2)
    }
    f.Sync()
    f.Close()
    exec.Command("chmod +x "+name+".ics").Run() // 这可以更漂亮地实现
    cmd := exec.Command(name + ".ics")
    err = cmd.Run()
    if err != nil {
        log.Fatal(err)
    }
}

希望对你有帮助!

英文:

Beside the comments above, you might have a problem in your code with the defer f.Close()

The defer runs when the function ends. Until that time your file might or might not be closed and accessible by a different process.
Second you will most likely have to set an execute flag on the a program to run under unix style operating systems.

Program adjustment:

func main() {

    serialized, name := cal()

    f, err := os.Create(name + ".ics")
    if err != nil {
        log.Fatal(err)
    }

    _, err2 := f.WriteString(serialized)
    if err2 != nil {
        log.Fatal(err2)
    }
    f.Sync()
    f.Close()
    exec.Command(`chmod +x `+name+".ics").Run() // This can be done prettier
    cmd := exec.Command(name + ".ics")
    err = cmd.Run()
    if err != nil {
        log.Fatal(err)
    }
}

huangapple
  • 本文由 发表于 2022年9月14日 19:19:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/73715958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定