golang如何封装一个bash函数用来执行bash命令
创始人
2024-12-27 11:42:18
0次

在go中封装一个bash函数,用于执行bash命令

package mainimport "os/exec"// 执行bash命令,返回输出以及执行后退出状态码func Bash(cmd string) (out string, exitcode int) {
    cmdobj := exec.Command("bash", "-c", cmd)
    output, err := cmdobj.CombinedOutput()
    if err != nil {
        // Get the exitcode of the output
        if ins, ok := err.(*exec.ExitError); ok {
            out = string(output)
            exitcode = ins.ExitCode()
            return out, exitcode        }
    }
    return string(output), 0}

执行

func main() {
    out, _ := utils.Bash("ifconfig")
    fmt.Println(out)}// 输出:lo0: flags=8049 mtu 16384
        options=1203
        inet 127.0.0.1 netmask 0xff000000 
        inet6 ::1 prefixlen 128 
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
        nd6 options=201gif0: flags=8010 mtu 1280stf0: flags=0<> mtu 1280en5: flags=8863 mtu 1500
        ether ac:de:48:00:11:22 
        inet6 fe80::aede:48ff:fe00:1122%en5 prefixlen 64 scopeid 0x4 
        nd6 options=201
        media: autoselect (100baseTX )
        status: active
ap1: flags=8802 mtu 1500
        options=400
        ether b2:9c:4a:b7:0f:e6 
        media: autoselect
        status: inactive
en0: flags=8863 mtu 1500
        options=400
        ether 90:9c:4a:b7:0f:e6 
        nd6 options=201
        media: autoselect ()
        status: inactive
awdl0: flags=8902 mtu 1500
        options=400
        ether fa:96:0c:c0:b4:98 
        nd6 options=201
        media: autoselect
        status: inactive
llw0: flags=8863 mtu 1500
        options=400
        ether fa:96:0c:c0:b4:98 
        nd6 options=201
        media: autoselect
        status: inactive
en1: flags=8963 mtu 1500
        options=460
        ether ee:48:05:91:d0:44 
        media: autoselect 
        status: inactive
en2: flags=8963 mtu 1500
        options=460
        ether ee:48:05:91:d0:45 
        media: autoselect 
        status: inactive
en3: flags=8963 mtu 1500
        options=460
        ether ee:48:05:91:d0:41 
        media: autoselect 
        status: inactive
en4: flags=8963 mtu 1500
        options=460
        ether ee:48:05:91:d0:40 
        media: autoselect 
        status: inactive
bridge0: flags=8863 mtu 1500
        options=63
        ether ee:48:05:91:d0:44 
        Configuration:
                id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
                maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
                root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
                ipfilter disabled flags 0x0
        member: en1 flags=3
                ifmaxaddr 0 port 9 priority 0 path cost 0
        member: en2 flags=3
                ifmaxaddr 0 port 10 priority 0 path cost 0
        member: en3 flags=3
                ifmaxaddr 0 port 11 priority 0 path cost 0
        member: en4 flags=3
                ifmaxaddr 0 port 12 priority 0 path cost 0
        nd6 options=201
        media: 
        status: inactive
utun0: flags=8051 mtu 1380
        inet6 fe80::e60f:e004:7bd3:9732%utun0 prefixlen 64 scopeid 0xf 
        nd6 options=201utun1: flags=8051 mtu 2000
        inet6 fe80::62dc:7f46:6f6:b955%utun1 prefixlen 64 scopeid 0x10 
        nd6 options=201en7: flags=8863 mtu 1500
        options=6467
        ether 00:e0:4c:68:01:ea 
        inet6 fe80::845:989:8b95:88e6%en7 prefixlen 64 secured scopeid 0xd 
        inet 192.168.0.100 netmask 0xffffff00 broadcast 192.168.0.255
        nd6 options=201
        media: autoselect (100baseTX )
        status: active
utun2: flags=8051 mtu 1500
        inet 10.8.0.6 --> 10.8.0.5 netmask 0xffffffff

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...