bug:golang通过exec.Command()执行命令报错

bug:golang通过exec.Command()执行命令报错

1 通过exec指定zip命令报错

需求描述:压缩某个目录下的所有文件

  • 在执行过程中,发现zip命令可行,但是 /usr/bin/zip test.zip *发现无法压缩成功,程序直接报错退出,后来排查返现是golang中的exec.Command()不支持*通配符,但是我们可以通过下面的方式实现效果。

*替换为所有的文件名及目录:

go 复制代码
package main

import (
	"fmt"
	"os/exec"
	"path/filepath"
)

func main() {
	zipName := "9999.zip"
	command := []string{
		"-r",
		zipName,
	}
	tmp, err := filepath.Glob("*")
	if len(tmp) == 0 {
		fmt.Println("No matching files found")
	}
	fmt.Println("* tmp=", tmp)
	command = append(command, tmp...)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("args=", command)

	cmd := exec.Command("/usr/bin/zip", command...)
	fmt.Println("compression cmd=", cmd)
	err = cmd.Run()
	if err != nil {
		fmt.Println("compression dir err=", err)
		return
	}

}
相关推荐
李永奉3 小时前
杰理可视化SDK开发-【BUG】配置“TWS两边同时按消息使能”功能后,按键单击功能无效失灵
开发语言·单片机·嵌入式硬件·物联网·bug
tiana_5 小时前
写了个零依赖的 Go 版本管理器,curl | bash 完事
开发语言·golang·bash
灯澜忆梦7 小时前
GO---map函数
开发语言·前端·后端·golang
AI大模型-小雄8 小时前
充值Codex + GPT-5.5 并发 Bug 调试实战:从时序分析到回归测试
gpt·log4j·bug
北冥you鱼8 小时前
Go语言与默克尔树:区块链数据完整性的基石
开发语言·golang·区块链
布朗克16810 小时前
Go 入门到精通-15-错误处理
开发语言·python·golang·错误处理
北冥you鱼12 小时前
Go-Ethereum (Geth) 最佳实践:从部署到生产环境优化
开发语言·后端·golang
穷人小水滴1 天前
(科幻) 备用肉身虫 (Backup Body Bug, BBB) 系列设定 (202607 更新)
aigc·bug·科幻
李燚1 天前
Eino devops 调试系统源码:GraphCompileCallback 怎么工作(第50篇-E36)
数据库·golang·agent·devops·graphql·aiagent·eino
名字还没想好☜1 天前
Go 内存逃逸分析:什么时候变量跑到堆上
开发语言·算法·性能优化·golang·go·内存逃逸