Source File
file.go
Belonging Package
mvdan.cc/sh/v3/fileutil
// Copyright (c) 2016, Daniel Martà <mvdan@mvdan.cc>// See LICENSE for licensing information// Package fileutil allows inspecting shell files, such as detecting whether a// file may be shell or extracting its shebang.package fileutilimport ()var (shebangRe = regexp.MustCompile(`^#!\s?/(usr/)?bin/(env\s+)?(sh|bash|mksh|bats|zsh)(\s|$)`)extRe = regexp.MustCompile(`\.(sh|bash|mksh|bats|zsh)$`))// TODO: consider removing HasShebang in favor of Shebang in v4// HasShebang reports whether bs begins with a valid shell shebang.// It supports variations with /usr and env.func ( []byte) bool {return Shebang() != ""}// Shebang parses a "#!" sequence from the beginning of the input bytes,// and returns the shell that it points to.//// For instance, it returns "sh" for "#!/bin/sh",// and "bash" for "#!/usr/bin/env bash".func ( []byte) string {:= shebangRe.FindSubmatch()if == nil {return ""}return string([3])}// ScriptConfidence defines how likely a file is to be a shell script,// from complete certainty that it is not one to complete certainty that// it is one.type ScriptConfidence intconst (// ConfNotScript describes files which are definitely not shell scripts,// such as non-regular files or files with a non-shell extension.ConfNotScript ScriptConfidence = iota// ConfIfShebang describes files which might be shell scripts, depending// on the shebang line in the file's contents. Since CouldBeScript only// works on os.FileInfo, the answer in this case can't be final.ConfIfShebang// ConfIsScript describes files which are definitely shell scripts,// which are regular files with a valid shell extension.ConfIsScript)// CouldBeScript is a shortcut for CouldBeScript2(fs.FileInfoToDirEntry(info)).//// Deprecated: prefer CouldBeScript2, which usually requires fewer syscalls.func ( os.FileInfo) ScriptConfidence {return CouldBeScript2(fs.FileInfoToDirEntry())}// CouldBeScript2 reports how likely a directory entry is to be a shell script.// It discards directories, symlinks, hidden files and files with non-shell// extensions.func ( fs.DirEntry) ScriptConfidence {:= .Name()switch {case .IsDir(), [0] == '.':return ConfNotScriptcase .Type()&os.ModeSymlink != 0:return ConfNotScriptcase extRe.MatchString():return ConfIsScriptcase strings.IndexByte(, '.') > 0:return ConfNotScript // different extensiondefault:return ConfIfShebang}}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |