The original link

Almost all programmers have more or less come into contact with shell scripts. In the process of using shell scripts, we will encounter some pain points, such as execution mode and usage method.

In this article, we’ll take a brief look at these pain points, and finally offer an elegant solution.

Implement way

There are two main ways to execute scripts:

  • Path calls, such as:./path/to/script.sh,sh ./path/to/script.sh
  • Global calls, such as writing shell script code directly~/.zshrcOr usealiasSpecify a concrete script path to implement the global invocation.

However, these two ways are obviously not elegant, let’s analyze it in detail.

Path to the call

Most people execute scripts this way. The advantage of path invocation is that the operation is easy to understand; But the downside is obvious — we need to know in advance where each script is in the file system.

In the case of cross-invocation, it is difficult to operate over a relative path and can only be invoked over an absolute path. For example: in ~ / chuquan/Develop/A/cut/chuquan/with making/B/script. Sh, can only be invoked by an absolute path.

The global call

In order to solve the problem of path invocation, some people may use the following methods.

The first method is to write the code of the shell script to a shell configuration file, such as ~/.zshrc. For example, by adding the following function to the end of ~/.zshrc and then executing source ~/.zshrc or restarting the terminal, we can call the shell script Chuquan globally.

# ~/.zshrc end
func chuquan() {
    echo "Hello, CHUQUAN"
}
Copy the code
Call chuquan globally
$ chuquan
Hello, CHUQUAN
Copy the code

The principle of the above method is that when the terminal is started, the system loads the corresponding configuration file according to the specified shell. If the default shell is ZSH, the ~/.zshrc configuration file is loaded. We write shell functions in ~/.zshrc to become global functions, thus implementing global calls to shell scripts.

The main disadvantages of this approach are:

  • ~/.zshrcCannot have a script function with the same name in the. If it does, the latter will override the former.
  • If there is too much script code, it can cause~/.zshrcThe file gets bigger and is eventually loaded when the shell starts~/.zshrcThe time will be correspondingly longer.

The second way is to use alias to define an alias for each script and its absolute path. For example, using the alias command to specify an alias hello for ~/chuquan/hello.sh to implement the global call.

$ alias hello=~/chuquan/hello.sh
Copy the code

This approach solves the drawbacks of the first approach. It wasn’t elegant at the time, and if there were a lot of scripts, there would be a lot of aliases, and for the user, would we really remember them all?

Method of use

In terms of usage, it’s often the case that someone has written a really cool script and we want to use it without knowing exactly what it does or even what parameters it takes to execute it. In this case, we often miss out on scripting tools that could improve productivity. The best solution to this is for the script author to provide usage hints and parameter hints in the script.

NOX

To solve the above problem, I wrote a zSH-based script management tool – NOX.

NOX mainly has the following characteristics:

  • The system callsNOX implements a unique master commandnoxAnd convert the user script storage path and script name into subcommands, so as to achieve global invocation. For example: throughnox poker aceCommand to invokescripts/poker/ace.shThe script.
  • Automatic completion: If the user manually enters similarnox poker aceSuch commands can be time-consuming, and NOX implements an elegant solutionTab auto-completion is supported. Automatic Tab completion speeds up command indexing and invocation.
  • The help option: To solve the problem that users don’t know how scripts are used, NOX providesnox system createCommand to quickly create a default support for developers--help(or-h) option script template. The script developer only needs to modify the description of the script usage in the script template.
  • Debug modeNOX is supported by default in the script template for easy script debugging--debug(or-x) option to easily switch script execution mode to debug mode.
  • Private command: NOX can be used as a development team toolbox, and in order to support private commands, NOX will not be used by default_Add git management for prefixed directory or script names.

Use the sample

The figure below shows an example of calling scripts/poker/ace.sh through NOX. In this example, the nox poker ace –count 10 command is used, with TAB auto-completion for quick indexing, and finally scripts/poker/ace.sh are called, passing in the script option –count and parameter 10.

Isn’t that cool?

System commands

NOX is essentially a script management tool, and its goal is to serve scripts. To this end, NOX provides three built-in system commands for users to use.

Create a directory/script

NOX provides NOX system create. This command can be used to create a template directory or template script.

  • nox system create --dir <dirname>: You can create a script directory, and one will be created in this directory by default.descriptionFile that describes the functions of scripts under this directory category.
  • nox system create --script <script-name-without-suffix>: You can create a template script that complies with the NOX development specification by default. You only need to modify the script to customize it.

Build autocomplete logic

The core of the auto-complete function is an auto-complete file that describes the auto-complete logic. For this, NOX provides a command NOX system build to compile and generate the corresponding auto-complete file. This command generates a _nox file and stores it in the fpath/ directory.

After we create a directory or script, we need to execute the nox system build command to generate a new autocomplete file. We then need to execute source ~/.zshrc or restart the terminal to update the auto-complete logic.

update

NOX can be used for team collaboration, and when other developers contribute some new scripts, we can update the NOX warehouse by performing the NOX System Update to enjoy the latest features.

Development of guidelines

NOX can write their own scripts into system commands, this is a very cool feature, you must want to try it. Here is a simple example detailing how to develop a script that meets the NOX specification, see Portal.

conclusion

NOX provides an elegant solution for managing shell scripts, converting shell scripts into system commands and providing powerful auto-completion capabilities. Welcome to install, use and put forward modification suggestions ~

reference

  1. nox