Giter VIP home page Giter VIP logo

bash-include's Introduction

Readme

Quick start guide

wget https://raw.githubusercontent.com/awesomefireduck/bash-include/master/include.bash -O $HOME/bin/include.bash
or
curl https://raw.githubusercontent.com/awesomefireduck/bash-include/master/include.bash -o $HOME/bin/include.bash

Then source the file in every script you want to include in. Like so:

#!/bin/bash
source $HOME/bin/include.bash #or full path

What it does

With this sourced into your script you can use the include statement to include a script from

Tutorial / Examples

1. Preparation

The file is located at ~/bin/include.bash

First we make some files:

~/bin/include/colours.sh a file containing variables we want to include in another script

#!/bin/bash

apple="sour green"
pepper="spicy red"

~/my_testscript the actual script we want to include stuff in

#!/bin/bash

# so we can use 'include'
source $HOME/bin/include.bash

# print the values of these variables (which are not yet defined)
echo "$apple"
echo "$pepper"

Now we run chmod +x ~/bin/my_testscript so we can run our script

2. include a file

We are now able to run ~/my_testscript and see what the output is.
You should get three empty lines since none of the variables should have a value.

Now we add an include to ~/my_testscript so that it looks like this:

#!/bin/bash

# so we can use 'include'
source $HOME/bin/include.bash

# include the colour variables
include colours.sh

# print the values of these variables
echo "$apple"
echo "$pepper"

It should output the following, each on it's own line: sour green
spicy red

insert first looks for the file (colours.sh) in the same dir as the script it's in. If it can't find it there it looks in ~/bin/include, and then in ~/bin. This means include found it at ~/bin/include/colours.sh. That's where our script's variables got their values.

3. Local first

Let's create a new file: ~/colours.sh

#!/bin/bash

apple="crunchy red"
pepper="sweet green"

Already feel where we're going? great! If not, that's okay too.

Let's see what the script prints when we run it:
crunchy red
sweet green

Do you remember the order in which include looks for the file? (see # 2.)
It already found it right next to ~/my_testscript, so it uses that and stops.

4. Overriding local include

If you do want to have include use the one in ~/bin/include (or if not found there, the one in ~/bin), you'll have to use the +local option like so:
~/my_testscript

#!/bin/bash

# so we can use 'include'
source $HOME/bin/include.bash

# include the colour variables
include colours.sh +local     # <--

# print the values of these variables
echo "$apple"
echo "$pepper"

If we now run ~/my_testscript it will output just like in the second part:
sour green
spicy red

It seems counter-intuitive to use a + to disable an option, but this is consistent with bash's options like set -e #exit on error and set +e #not exit on error

That's it folks!

Short roadmap:

  • more than one file per include statement
  • option for both so that it not stops if found locally
  • option to check local later than global (goes with ^?)
  • DONE! locally checking next to file included in (instead of PWD)
  • DONE! actually using ~/bin/include/ (before looking in ~/bin/)

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.