GithubHelp home page GithubHelp logo

bash_module's Introduction

bash-module

δΈ­ζ–‡

This is a bash script writing specification, and the library created under this specification, its main purpose is to solve the modularization problem of bash script

Why

When I use more and more bash scripts, I find it more convenient, basically all *inix can be executed without additional dependencies. For example, if you want to run python or js in a docker, you also need a script running environment. The python or nodejs environment will make the docker bloated, but using bash script will not have these problems. In addition, there are environments such as msys that can run bash even on windows, which makes the usable range of bash wider.

However, bash does not have a good modularization scheme, so it becomes difficult to reuse code. Fortunately, bash is still flexible. Modularization can be achieved as long as it follows some specifications and encapsulates a little, so I created some modularization Specification and provides the basic package core.sh for this

Rule

  1. A script defines a module. The variables and functions provided by the module should have a uniform name prefix to avoid name conflicts. The name prefix is called namespace

    For example, a string processing module, its namespace can be strings

    # define variable
    strings_Version="v1.0.0"
    
    # Define the trim function
    function strings.trim
    {}
    
  2. namespace should be all lowercase English words, usually the same as the file path stored in it. Multiple words are connected by _, for example:

    • strings
    • cache
    • cache_lru
    • cache_lru_timer
  3. Modules should define a _module_flag_ofnamespace variable at the beginning of the code to avoid being imported multiple times

    if [[ "$__module_flag_of_core_colors" == 1 ]];then
        return 0
    fi
    __module_flag_of_core_colors=1
    
  4. Use camel case for global variable names and function names. Public variables and functions should be capitalized, and private ones should be lowercase.

  5. Global variable name should use _ connection name and namespace, private should use __ connection name and namespace to avoid the final name and other namespace duplication

    # private variable
    strings__var
    # public variable
    strings_Var
    
  6. Public functions should use . connection name and namespace, and private functions should use ._ connection name and namespace to avoid calling a public method with a wrong lowercase initial letter and calling a private method with the same name

    # private method
    function strings._hasPrefix
    {}
    # public method
    function strings.HasPrefix
    {}
    

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.