Bashy is a script collector for bash system. It allows to download and collect scripts, and facilitates argument parsing.
Bashy provides a easy way for resolving arguments and generating an useful Help. You can simply have variable filled with values entered by the user by default, and the help infos build automatically from the script definition. This transofrms your bash script in a real console application with no pain
The installation package is available for windows and linux. It was tested on Window 10&11, Ubuntu, Debian and Alpine. If the binay is not compatible with your OS or processor, you can compile it from source (see next section).
Copy the next piece of code and run in your terminal.
curl -LO https://github.com/zeppaman/bashy/releases/download/latest/bashy.linux && install -m 0755 ./bashy.linux /usr/bin/bashy && rm ./bashy.linux && echo 'export PATH="$PATH:$HOME/.bashy/bin"'>>~/.bashrc && echo 'export PATH="$PATH:$HOME/.bashy/bin"'>>~/.profile && echo 'export PATH="$PATH:$HOME/.bashy/bin"'>>/etc/environment && export PATH="$PATH:$HOME/.bashy/bin"
note: basing on your os you may need to be root to run the command. Requires curl installed.
Open a powershell console with elevated privileges and run:
$bashyHome=$([Environment]::GetFolderPath("UserProfile"))+"\.bashy"; New-Item -ItemType Directory -Force -Path $bashyHome\bin; (New-Object System.Net.WebClient).DownloadFile('https://github.com/zeppaman/bashy/releases/download/latest/bashy.windows', "$bashyHome\bin\bashy.exe"); $old = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User); $env:Path= "$old;$bashyHome\bin"; [Environment]::SetEnvironmentVariable("Path", "$old;$bashyHome\bin", [System.EnvironmentVariableTarget]::User);
git clone <this repo url> bashy
cd bashy
bash ./install.sh
The script build executables in out folder. Copy them where you want and make sure to add the binary location on PATH and the /bin subfloder of BASHY_HOME folder (default is .bashy inside you home/profile folder)
installation notes:
- this compile bashy form source, so you can delete the folder at the end
- the tool download and install go if not present. If present, nothing is touched but it have to be updated manually to the 1.19 version.
git clone <this repo url> bashy
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; ./install.ps1
#rm -rf bashy optional
Despite that golang can produce multiplatform output and that this app could be improved to support multiple script engines (js,c#, etc..) and OS (windows, mac), at the moment the application is working only with linux and bash scripts.
The script hash a YAML file associated with it, so you can define all the infos. In the following example we are defining a command named sample
with two parameters (name, surname
).
name: Name
description: the command description
argsusage: help text
params:
- name: "name"
desc: "enter your name"
- name: "surname"
desc: "enter your surname"
This will allow you to use directly the parameter names as named variables. So, your script will be able to do something similar to:
echo "$name"
echo "$surname"
echo "$name $surname"
Moreover, you will be able to list all the commands available, and for each commands the help usage.
Each yaml file can contains one or more command definition. Commands can be:
- embedded (the script is contained inside the yaml file definition)
- linked to an external files (local or remote)
You can work on-the-fly scripts on the current directory or move them to the internal report and make them available at OS level. Moreover you can publish scripts over HTTP and download it locally.
bashy repo add https://xxx.yy/path.yml
This will download all related scripts and save them locally. The home of bashy is .bashy
on the user's home.
You can run scripts on a foder witout installing them on bashy by specifing the BASHY_EXTRA
(additional folder where the app looks for yaml) and `BASHY_FILES (a list of files path to add).
BASHY_EXTRA=samples/home go run ./src/main.go
BASHY_FILES=myfile.yml,/var/scripts/myscript2.yml bashy
bashy repo add filepath.yml
Here some commands for usage.
bashy --help #list all commands with description
# NAME:
# Bashy - A new cli application
# USAGE:
# main [global options] command [command options] [arguments...]
# COMMANDS:
# command1
# command2
# GLOBAL OPTIONS:
# --help, -h show help (default: false)
bashy command1 --help # show infos about the command
# NAME:
# command1
# USAGE:
# bashy command1 [command options] [arguments...]
# DESCRIPTION:
# the command description
# OPTIONS:
# --name value enter your name
# --surname value enter your surname
# --help, -h show help (default: false)
bashy <command name> parameters args # execute the command
# the command output
Some samples about how to embed a script.
You can write a command using
name: Name
description: the command description
argsusage: help text
params:
- name: "name"
desc: "enter your name"
- name: "surname"
desc: "enter your surname"
cmd: |
echo "commmand"
env
echo "$name $surname"
or you can concatenate all commands in a single line if you want to keep things more hard to understand:
name: Name
description: the command description
argsusage: help text
params:
- name: "name"
desc: "enter your name"
- name: "surname"
desc: "enter your surname"
cmd: echo "commmand" && env echo "$name $surname"
In case you want to define multiple command you can define it by adding multiple cmds
nodes. Each one can cantain multiple statement like in the Singleline script case. Cmd and Cmds can coexists: Cmd is executed AFTER the Cmds list.
name: Name
description: the command description
argsusage: help text
cmd: echo "commmand" && env
params:
- name: "name"
desc: "enter your name"
- name: "surname"
desc: "enter your surname"
cmds:
- echo "$name"
- echo "$surname"
- echo "$name $surname"
An external script can be included. The external script is not exclusive: you can use it in conjunction with cmd, cmds
arguments, but loaded at the end. You can specify a local path (relative or absolute), or an URL. The remote script will be downloaded at the first usage then cached locally. Examples:
Remote script
script: https://gist.githubusercontent.com/zeppaman/bbdfbac1304f88df1b905692e42f4d4e/raw/22ab3a2163f6ea481bca1b5d5570a964120a4f89/test-bashy.sh
Local relative path The path is relative to the same location
script: ./test-bashy.sh
Local absolute path
script: /test-bashy.sh
Note: absolute paths are not changed during the import/execution process
The engine of bashy support any kind of script interpreter but it is shipped with sh for Linux and cmd for windows. To add a new one just create a file like the following (this piece of code add the nodejs interpreter):
name: node
params:
- node
- $filename
os: linux
installscript:
- #!/bin/bash
- echo "installing dependencies (node)"
- '[[ "$(command -v apt)" ]] || { apt install nodejs -y; }'
- exit 0
variabletemplate: var $name="$value";
Important fields are: name the name of the interpreter. Into script YAML you will set the interpreter parameter to match this.
- params a list of parameters to run the script. The placeholder
$filename
will be replaced with the temporary path of the script that will be generated during the script run. The list in the example will be transformed intonode /
path/to/file` - os the name of the os where the interpreter can be interpreted. If you have an interpreter that can be used in two different OS, you have to duplicate it
- installscript list of script steps for installing the interpreter. In the example above, it checks if apt is available, then installs node
- variabletemplate the template for adding variables on top of the script,
$name`` and
$value` are placeholders that will hold the param name and value as defined in the command YAML.
The installation of the interpreter can be done by using the interpreter
command, like in the following example:
bash interpreter add samples/interpreter.yml
you can check the list of the loaded interpreters by typing: Once the interpreter is installed, you can use it by adding the field `interpreter:
name: Name
description: the command description
argsusage: help text
cmd: ...
interpreter: node
Bashy ships a set of default interpreters based on the OS. The list is the following.
OS | Name | Default |
---|---|---|
LINUX | sh | YES |
WINDOWS | cmd | YES |
BASHY_HOME=samples bashy
BASHY_HOME=tmp/home go run ./src/main.go
BASHY_HOME=tmp/home BASHY_EXTRA=samples/home go run ./src/main.go