package sh

import "github.com/tredoe/osutil/sh"

Package sh interprets a command line just like it is done in the Bash shell.

The main function is Run which lets to call to system commands under a new process. It handles pipes, environment variables, and does pattern expansion.

Index

Constants

const PATH = "/sbin:/bin:/usr/sbin:/usr/bin"

Variables

var (
	BOOT  bool // does the script is being run during boot?
	DEBUG bool

	Log = log.New(ioutil.Discard, "", 0)
)
var Debug bool

Debug shows debug messages in functions like Run.

Functions

func CloseLogger

func CloseLogger() error

CloseLogger closes the log file.

func Run

func Run(command string) (output []byte, err error)

Run executes external commands just like RunWithMatch, but does not return the boolean `match`.

func RunWithMatch

func RunWithMatch(command string) (output []byte, match bool, err error)

RunWithMatch executes external commands with access to shell features such as filename wildcards, shell pipes, environment variables, and expansion of the shortcut character "~" to home directory.

This function avoids to have execute commands through a shell since an unsanitized input from an untrusted source makes a program vulnerable to shell injection, a serious security flaw which can result in arbitrary command execution.

The most of commands return a text in output or an error if any. `match` is used in commands like *grep*, *find*, or *cmp* to indicate if the serach is matched.

func RunWithMatchf

func RunWithMatchf(format string, args ...interface{}) ([]byte, bool, error)

RunWithMatchf is like RunWithMatch, but formats its arguments according to the format. Analogous to Printf().

func Runf

func Runf(format string, args ...interface{}) ([]byte, error)

Runf is like Run, but formats its arguments according to the format. Analogous to Printf().

func StartLogger

func StartLogger()

StartLogger initializes the log file.

Source Files

log.go sh.go

Version
v1.5.0 (latest)
Published
Jun 4, 2024
Platform
linux/amd64
Imports
12 packages
Last checked
1 day ago

Tools for package owners.