ZhangZhihui's Blog  

 

package main

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

func main() {
    args := os.Args
    if len(args) == 1 {
        fmt.Println("Please provide an argument!")
        return
    }

    file := args[1]
    path := os.Getenv("PATH")
    pathSplit := filepath.SplitList(path)

    for _, dir := range pathSplit {
        fullPath := filepath.Join(dir, file)
        // Does it exist?
        fileInfo, err := os.Stat(fullPath)
        if err != nil {
            continue
        }

        mode := fileInfo.Mode()
        // Is it a regular file?
        if !mode.IsRegular() {
            continue
        }

        // Is it executable?
        if mode&0111 != 0 {
            fmt.Println(fullPath)
            return
        }
    }
}

 

posted on 2024-06-10 15:27  ZhangZhihuiAAA  阅读(4)  评论(0编辑  收藏  举报