Skip to content

Instantly share code, notes, and snippets.

@alwqx
Last active February 7, 2017 10:22
Show Gist options
  • Save alwqx/ef538582f6001408321827c86d10c198 to your computer and use it in GitHub Desktop.
Save alwqx/ef538582f6001408321827c86d10c198 to your computer and use it in GitHub Desktop.
go text remove extra space of script output
package main
import(
"os/exec"
"os"
"fmt"
)
func main() {
devices := getDevices()
fmt.Println(devices)
fmt.Println(len(devices))
}
func getDevices() []string {
ret, err := exec.Command("/sbin/smartctl", "--scan").Output()
if err != nil {
log.Println("Execute /sbin/smartctl --scan error: %v", err)
os.Exit(-1)
}
linest := strings.Split(string(ret), "\n")
lines := linest[:len(linest)-1] // remove last extra line with output of bash command
var devList = []string{}
for _, line := range lines {
device := strings.Fields(line)[0]
devList = append(devList, device)
}
return devList
}
/*
remove extra line
[/dev/sda]
1
not remove extra line
[/dev/sda ]
2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment