Code Monkey home page Code Monkey logo

bash-tools's Introduction

bash-tools

Set of includable tools to simplify and improve bash coding


dataset

Works with associative arrays; Before use you need to declare the variable as an associative array with declare -A myvar then pass to function the var name as a string.

The indexes must start with characters from A-Z, a-z and 0-9 and can have any characters except equal sign = and spaces

The values can have any characters, however spaces and tabs are removed from start and end of the string.

Gotchas

  • When using returned values for loops use always "${myvar[@]}" encapsulated in double quotes and using @ or you can have unexpected results.

dataset::read

Read a file converting its contents to an associative array;

File example in 'test/example.conf':

# It is a conf commentary line!
itsa.key1 = apple
itsa key2 = invalid line...
Key1      = remains = as = value
2key      = $HOME 

Call example:

declare -A X=$( dataset::read example.conf )
for i in "${!X[@]}"; do
	echo "Key: ($i) / Value: (${X[$i]})";
done

Results in:

Key: (2key) / Value: (/your/home/folder)
Key: (itsa.key1) / Value: (apple)
Key: (Key1) / Value: (remains = as = value)

dataset::match

Get all the indexes of an associative array starting with the string.

for i in $(dataset::match X 'itsa') ; do
	echo "Key: ($i) / Value: (${X[$i]})";
done

Results in:

Key: (itsa.key1) / Value (apple)

You can also use bash patterns on match:

for i in $(dataset::match X '[kK]ey' ) ; do
	echo "Key: ($i) / Value (${X[$i]})";
done

Results in:

Key: (key2) / Value ()
Key: (Key1) / Value (remains = as = value)

bash-tools's People

Contributors

dev-kxmn 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.