Giter Site home page Giter Site logo

globals's Introduction

Global Variables for Bash

a small library to provide a form of global variables in a bash shell

The library can be taken from the scripts folder, put in your folder with scripts, and dot-source in your scripts.

. ./.globals.bash

The functions provided:

  • global is used to set a global variable. It is used like one uses the export statement or the local statement in a function.

    global MY_VAR="some value"
    • The variable is set in the current environment.
    • The variable is exported in the current environment, so it is available in the environments of all future child-shells.
    • The variable is set in the environments of the chain of parent-shells that used call to eventually run this script.
    • The variable is exported in the environments of these parent-shells, so it is available in the environments of all future child-shells of these parent-shells.
  • call is used to execute a script, pickup all global variables set by that script and its call-ed child-scripts, and inject these global variables in the current environment.

    call ./my-script
    • call uses bash to execute the script
    • The exitcode of the executed script is returned in $?
    • A global variable $LASTEXITCODE is set to the $? value of the last call-ed script
    • A global variable $GLOBALS_INJECT is reserved to communicate the dynamically created filedescriptor of the inject-stream between calling and called scripts. This inject-stream is used to send the required information to set global variables, from the called script back to the calling script. This inject-stream is only open for as long as the called script is running.

Example

Let's make three scripts

  • test-globals-1.bash calls test-globals-2.bash

    . ./.globals.bash
    
    echo "### test-globals-1.bash"
    echo ""
    
    call ./test-globals-2.bash
    
    echo "\$?=$?"
    echo ""
    
    echo "### test-globals-1.bash"
    echo "\$LASTEXITCODE=$LASTEXITCODE"
    echo "\$MY_VAR=$MY_VAR"
    echo "###"
    echo ""
  • test-globals-2.bash calls test-globals-3.bash

    . ./.globals.bash
    
    echo "### test-globals-2.bash"
    echo ""
    
    call ./test-globals-3.bash
    
    echo "\$?=$?"
    echo ""
    
    echo "### test-globals-2.bash"
    echo "\$LASTEXITCODE=$LASTEXITCODE"
    echo "\$MY_VAR=$MY_VAR"
    echo "###"
    echo ""
    
    exit $LASTEXITCODE
  • test-globals-3.bash set a global variable

    . ./.globals.bash
    
    echo "### test-globals-3.bash"
    echo ""
    
    global MY_VAR="some value"
    
    echo "### test-globals-3.bash"
    echo "\$LASTEXITCODE=$LASTEXITCODE"
    echo "\$MY_VAR=$MY_VAR"
    echo "###"
    echo ""
    
    exit 42

Now run

./test-globals-1.bash

The result is:

### test-globals-1.bash

### test-globals-2.bash

### test-globals-3.bash

### test-globals-3.bash
$LASTEXITCODE=
$MY_VAR=some value
###

$?=42

### test-globals-2.bash
$LASTEXITCODE=42
$MY_VAR=some value
###

$?=42

### test-globals-1.bash
$LASTEXITCODE=42
$MY_VAR=some value
###

ℹī¸
The scripts folder contains a more elaborate version of the above scripts.


For Further Investigation

  • use the same techniques to return results to the caller of a script

globals's People

Contributors

stefaanc avatar

Stargazers

 avatar

Watchers

 avatar

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.