登录  /  注册
首页 > 后端开发 > Golang > 正文

关于golang封装一个bash函数,用于执行bash命令

藏色散人
发布: 2020-12-28 15:58:48
转载
2491人浏览过

下面由golang教程栏目给大家介绍关于golang封装一个bash函数,用于执行bash命令析,希望对需要的朋友有所帮助!

关于golang封装一个bash函数,用于执行bash命令

在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<up> mtu 16384
        options=1203<rxcsum>
        inet 127.0.0.1 netmask 0xff000000 
        inet6 ::1 prefixlen 128 
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
        nd6 options=201<performnud>gif0: flags=8010<pointopoint> mtu 1280stf0: flags=0 mtu 1280en5: flags=8863<up> mtu 1500
        ether ac:de:48:00:11:22 
        inet6 fe80::aede:48ff:fe00:1122%en5 prefixlen 64 scopeid 0x4 
        nd6 options=201<performnud>
        media: autoselect (100baseTX <full-duplex>)
        status: active
ap1: flags=8802<broadcast> mtu 1500
        options=400<channel_io>
        ether b2:9c:4a:b7:0f:e6 
        media: autoselect
        status: inactive
en0: flags=8863<up> mtu 1500
        options=400<channel_io>
        ether 90:9c:4a:b7:0f:e6 
        nd6 options=201<performnud>
        media: autoselect (<unknown>)
        status: inactive
awdl0: flags=8902<broadcast> mtu 1500
        options=400<channel_io>
        ether fa:96:0c:c0:b4:98 
        nd6 options=201<performnud>
        media: autoselect
        status: inactive
llw0: flags=8863<up> mtu 1500
        options=400<channel_io>
        ether fa:96:0c:c0:b4:98 
        nd6 options=201<performnud>
        media: autoselect
        status: inactive
en1: flags=8963<up> mtu 1500
        options=460<tso4>
        ether ee:48:05:91:d0:44 
        media: autoselect <full-duplex>
        status: inactive
en2: flags=8963<up> mtu 1500
        options=460<tso4>
        ether ee:48:05:91:d0:45 
        media: autoselect <full-duplex>
        status: inactive
en3: flags=8963<up> mtu 1500
        options=460<tso4>
        ether ee:48:05:91:d0:41 
        media: autoselect <full-duplex>
        status: inactive
en4: flags=8963<up> mtu 1500
        options=460<tso4>
        ether ee:48:05:91:d0:40 
        media: autoselect <full-duplex>
        status: inactive
bridge0: flags=8863<up> mtu 1500
        options=63<rxcsum>
        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<learning>
                ifmaxaddr 0 port 9 priority 0 path cost 0
        member: en2 flags=3<learning>
                ifmaxaddr 0 port 10 priority 0 path cost 0
        member: en3 flags=3<learning>
                ifmaxaddr 0 port 11 priority 0 path cost 0
        member: en4 flags=3<learning>
                ifmaxaddr 0 port 12 priority 0 path cost 0
        nd6 options=201<performnud>
        media: <unknown>
        status: inactive
utun0: flags=8051<up> mtu 1380
        inet6 fe80::e60f:e004:7bd3:9732%utun0 prefixlen 64 scopeid 0xf 
        nd6 options=201<performnud>utun1: flags=8051<up> mtu 2000
        inet6 fe80::62dc:7f46:6f6:b955%utun1 prefixlen 64 scopeid 0x10 
        nd6 options=201<performnud>en7: flags=8863<up> mtu 1500
        options=6467<rxcsum>
        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<performnud>
        media: autoselect (100baseTX <full-duplex>)
        status: active
utun2: flags=8051<up> mtu 1500
        inet 10.8.0.6 --&gt; 10.8.0.5 netmask 0xffffffff</up></full-duplex></performnud></rxcsum></up></performnud></up></performnud></up></unknown></performnud></learning></learning></learning></learning></rxcsum></up></full-duplex></tso4></up></full-duplex></tso4></up></full-duplex></tso4></up></full-duplex></tso4></up></performnud></channel_io></up></performnud></channel_io></broadcast></unknown></performnud></channel_io></up></channel_io></broadcast></full-duplex></performnud></up></pointopoint></performnud></rxcsum></up>
登录后复制

更多相关技术文章,请访问go语言教程栏目!                                                        

以上就是关于golang封装一个bash函数,用于执行bash命令的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:learnku网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号