# Auto Mount ###### tags: `go`, `script` ``` go package main import ( "fmt" "os" "os/exec" "regexp" "string" ) func main() { fmt.Println("os dev UUID") cmd := `blkid -s UUID` blkid := runCmd(cmd) devices := scans(blkid, `\/dev\/(.*): UUID=\"(.*)\"`, 1, 2) for _, device := range devices { path := fmt.Sprintf(`/tmp/%s/`, device.Name) // 建立資料夾 cmd = fmt.Sprintf(`mkdir %s`, path) runCmd(cmd) // 掛載 cmd = fmt.Sprintf(`mount /dev/%s %s`, device.Name, path) runCmd(cmd) // 判斷有無作業系統 switch { case fileExist(path + `Windows`): device.OS = `windows` case fileExist(path + `root`): device.OS = `linux` } device.Show() } fmt.Println() fmt.Println(`/tmp/[device]`) } type Device struct { Name string UUID string OS string } func (device Device) Show() { os := rjust(device.OS, 7, "") fmt.Println(os, device.Name, device.UUID) } func scans(str, keyword string, is ...int) []Device { re := regexp.MustCompile(keyword) matchs := re.FindStringSubmatch(str) var devices []Device for _, match := range matchs { var device Device device.Name = match[1] device.UUID = match[2] devices = append(devices, device) } return devices } func fileExist(file string) bool { if _, err := os.Stat(file); err != nil { if os.IsNotExist(err) { return false } } return true } func runCmd(cmd string) string { c, _ := exec.Command(`bash`, `-c`, cmd).CombinedOutput() return string(c) } func rjust(str string, n int, fill string) string { if len(str) < n { return strings.Repeat(fill, n-len(str)) + str } return str } ```