gsh

package
v1.16.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2026 License: Apache-2.0 Imports: 6 Imported by: 4

README

gsh - An alternative to write shell scripts

Language GitHub release Discord GoDoc

This is an alternative to write shell scripts.

Yes, now you can write shell script in XGo. It supports all shell commands.

Usage

First, let's create a file named example.gsh and write the following code:

mkdir "testgsh"

You don't need a go.mod file, just enter xgo run ./example.gsh directly to run.

It's strange to you that the file extension of XGo source is not .xgo but .gsh. It is only because XGo register .gsh as a builtin classfile.

We can change example.gsh more complicated:

type file struct {
	name  string
	fsize int
}

mkdir! "testgsh"

mkdir "testgsh2"
lastErr!

mkdir "testgsh3"
if lastErr != nil {
	panic lastErr
}

capout => { ls }
echo output.fields

capout => { ls "-l" }
files := [file{flds[8], flds[4].int!} for e <- output.split("\n") if flds := e.fields; flds.len > 2]
echo files

rmdir "testgsh", "testgsh2", "testgsh3"
Execute shell commands

There are many ways to execute shell commands. The simplest way is:

mkdir "testgsh"

It is equivalent to:

exec "mkdir", "testgsh"

or:

exec "mkdir testgsh"

If a shell command is a Go/XGo language keyword (eg. go), or the command is a relative or absolute path, you can only execute it in the latter two ways:

exec "go", "version"
exec "./test.sh"
exec "/usr/bin/env xgo run ."

You can also specify environment variables to run:

exec "GOOS=linux GOARCH=amd64 go install ."
Retrieve environment variables

You can get the value of an environment variable through ${XXX} or $XXX. For example:

echo ${HOME}
ls $HOME

You can also use exec command through $XXX:

exec "ls $HOME"
Check last error

If we want to ensure mkdir successfully, there are three ways:

The simplest way is:

mkdir! "testsh"  # will panic if mkdir failed

The second way is:

mkdir "testsh"
lastErr!

Yes, gsh provides lastErr to check last error.

The third way is:

mkdir "testsh"
if lastErr != nil {
    panic lastErr
}

This is the most familiar way to Go developers.

Capture output of commands

And, gsh provides a way to capture output of commands:

capout => {
    ...
}

Similar to lastErr, the captured output result is saved to output.

For example:

capout => { ls "-l" }
echo output

Here is a possible output:

total 72
-rw-r--r--  1 xushiwei  staff  11357 Jun 19 00:20 LICENSE
-rw-r--r--  1 xushiwei  staff    127 Jun 19 10:00 README.md
-rw-r--r--  1 xushiwei  staff    365 Jun 19 00:25 example.gsh
-rw-r--r--  1 xushiwei  staff    126 Jun 19 09:33 go.mod
-rw-r--r--  1 xushiwei  staff    165 Jun 19 09:33 go.sum
-rw-r--r--  1 xushiwei  staff   1938 Jun 19 10:00 xgo_autogen.go

We can use XGo powerful built-in data processing capabilities to process captured output:

type file struct {
	name  string
	fsize int
}

files := [file{flds[8], flds[4].int!} for e <- output.split("\n") if flds := e.fields; flds.len > 2]

In this example, we split output by "\n", and for each entry e, split it by spaces (e.fields) and save into flds. Condition flds.len > 2 is to remove special line of output:

total 72

At last, pick file name and size of all selected entries and save into files.

Documentation

Index

Constants

View Source
const (
	XGoPackage = true
)

Variables

This section is empty.

Functions

func Getenv added in v1.13.9

func Getenv(env []string, name string) string

Getenv retrieves the value of the environment variable named by the key. It returns the value, which will be empty if the variable is not present. To distinguish between an empty value and an unset value, use LookupEnv.

func InitApp added in v1.15.2

func InitApp(app *App)

InitApp initializes an App instance. It is provided so that an App instance embedded in a struct can be initialized from another package.

func Setenv__0 added in v1.13.9

func Setenv__0(ret []string, env map[string]string) []string

Setenv overwrites environments with specified env.

func Setenv__1 added in v1.13.9

func Setenv__1(ret []string, name, val string) []string

Setenv overwrites environments with specified (name, val) pair.

func Setenv__2 added in v1.13.9

func Setenv__2(ret []string, env []string) []string

Setenv overwrites environments with specified "name=val" pairs.

func XGot_App_Main added in v1.16.0

func XGot_App_Main(a interface{ initApp() })

XGot_App_Main is main entry of this classfile.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

App is project class of this classfile.

func (*App) Capout

func (p *App) Capout(doSth func()) (string, error)

Capout captures stdout of doSth() execution and save it to output.

func (*App) Exec__0 added in v1.13.9

func (p *App) Exec__0(env map[string]string, name string, args ...string) error

Exec executes a shell command with specified environs.

func (*App) Exec__1 added in v1.13.9

func (p *App) Exec__1(cmdline string) error

Exec executes a shell command line with $env variables support.

  • exec "XGO_GOCMD=tinygo xgo run ."
  • exec "ls -l $HOME"

func (*App) Exec__2 added in v1.13.9

func (p *App) Exec__2(name string, args ...string) error

Exec executes a shell command.

func (*App) ExitCode added in v1.13.9

func (p *App) ExitCode() int

ExitCode returns exit code of last command execution. Bash-scripting exit codes: 1: Catchall for general errors 2: Misuse of shell builtins (according to Bash documentation) 126: Command invoked cannot execute 127: Command not found 128+n: Fatal error signal "n" 254: Unknown error(*)

func (*App) LastErr

func (p *App) LastErr() error

LastErr returns error of last command execution.

func (*App) Output

func (p *App) Output() string

Output returns result of last capout.

func (*App) XGo_Env added in v1.16.0

func (p *App) XGo_Env(key string) string

XGo_Env retrieves the value of the environment variable named by the key.

func (*App) XGo_Exec added in v1.16.0

func (p *App) XGo_Exec(name string, args ...string) error

XGo_Exec executes a shell command.

type OS added in v1.13.9

type OS interface {
	// Environ returns a copy of strings representing the environment,
	// in the form "key=value".
	Environ() []string

	// ExpandEnv replaces ${var} or $var in the string according to the values
	// of the current environment variables. References to undefined
	// variables are replaced by the empty string.
	ExpandEnv(s string) string

	// Getenv retrieves the value of the environment variable named by the key.
	// It returns the value, which will be empty if the variable is not present.
	// To distinguish between an empty value and an unset value, use LookupEnv.
	Getenv(key string) string

	// Run starts the specified command and waits for it to complete.
	Run(c *exec.Cmd) error
}
var Sys OS = defaultOS{}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL