Git Product home page Git Product logo

bash-import-source's Introduction

bash-imports

This is a small bash script I wrote to help with reusing shell code. It provides a shell function named import for importing "modules", which is basically a more general version of source. In fact, import ultimatly just calls source, but it searches for the file (or files) to source. For example:

#! /bin/bash
source import_utils
import functions

...

In this script, the import function will look for a file or directory named:

  1. functions
  2. functions.sh
  3. functions.tar
  4. functions.tar.bz2
  5. functions.tar.gz

If found, the import function will do the following:

  1. If a file is found, it is sourced.
  2. If a directory is found:
    1. Source all *.sh files in the top-level of the directory.
    2. Source all *.sh files in the lib/ subdirectory, if it exists.
    3. Add the bin/ subdirectory to PATH.
  3. If a tarball is found:
    1. Unpack the tarball into a directory.
    2. Source all *.sh files in the top-level of the directory.
    3. Source all *.sh files in the lib/ subdirectory, if it exists.
    4. Add the bin/ subdirectory to PATH.

The module function will search for modules in the current directory first. If the module is not found, it will start searching in the list of directories contained in the IMPORT_MODULE_PATH (set to ${HOME}/.bash/modules by default) environment variable. The first file or directory found is used.

This makes it very easy to reuse utility code in your shell scripts. Just wrap the code up into shell functions, save them to a file, and drop it in ${HOME}/.bash/modules. Then your script can source the import_utils script, and import the file containing your utility code.

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.