blog

Major bashlib Updates

2014-04-02

I've made a significant number of changes to bashlib today, which felt right as it's now 1 year old!

The primary change is the structure, functions are not as scattered as they were and consist of a fake "class" name preceeding them.

As an example, the function "len" was renamed to "string.len", "ceil" was renamed to "math.ceil" (despite the fact I dislike the Americanism on the word maths, shorter tends to be better and it's a standard in many languages).

I've also added a significant number of functions in, mainly functions that do very generic stuff.

Again, primarly this was a set of array functions that let you create, destroy and modify arrays in a much more convinient and simplistic way. For example, to create two arrays, modify them, concate them and then assign the concated array to a variable ready for use in something else, you can do:

array.create "array_a" "value_a1" "value_a2"
list_array_a=$( array.list "array_a" )
array.create "array_b" "value_b1" "value_b2"
list_array_b=$( array.list "array_b" )
array.concate "array_c" "array_a" "array_b"
some_variable_a=$( array.list "array_c" )
array.append "array_c" "value_a3" "value_a4"
some_variable_b=$( array.list "array_c" )

You can even sort the lists (only alphanumerically at the moment), prepend data and modify specfic indexes.

array.prepend "array_c" "value_c1" "value_c2"
some_variable_c=$( array.list "array_c" )
array.amend "array_c" "2" "value_c2_new"
some_variable_d=$( array.list "array_c" )
array.sort "array_c" "asc"
some_variable_e=$( array.list "array_c" )

If you echo the steps, you can see what happens:

echo -e "Array A: $list_array_a"
echo -e "Array B: $list_array_b"
echo -e "Concate: $some_variable_a"
echo -e "Append:  $some_variable_b"
echo -e "Prepend: $some_variable_c"
echo -e "Amend:   $some_variable_d"
echo -e "Sort:    $some_variable_e"

---

$ bash arrays.sh
Array A: value_a1 value_a2 
Array B: value_b1 value_b2 
Concate: value_a1 value_a2 value_b1 value_b2 
Append:  value_a1 value_a2 value_b1 value_b2 value_a3 value_a4 
Prepend: value_c1 value_c2 value_a1 value_a2 value_b1 value_b2 value_a3 value_a4 
Amend:   value_c1 value_c2 value_a1 value_a2 value_c2_new value_b2 value_a3 value_a4 
Sort:    value_a1 value_a2 value_a3 value_a4 value_b2 value_c1 value_c2 value_c2_new

This may now be ideal for everyone, but I've tried to make these functions pretty angnostic, they shouldn't require quotes, and will do all their working out using whatever IFS is set before hand (as in, they don't declare their own IFS) if you need spaces or whatever in the array variables.

As another point, I've gone back through all other functions and swapped any "[[ x -eq y ]]" tests for the more appropriate arithatic "(( x == y ))". Also, almost all functions will return values via a single "printf" statement, so you can enclose them in to a variable (like a C function) without much hassle (just use a subshell). There are a couple of functions, like the "*_parser" functions that still return an array, because there really is no point in not as most of what they do is shoving standard text into an array.

A final note, the minify script is now working properly again, and you can use "./bashlib.sh -v" (or "./bashlib.min.sh") to pull version information.

As usual, get it on git hub (here), or download it from the programming page as they are both up to date (as of 14/04/02).

Edit (14/04/04):

Did some more work, and now I have a pair of simple array search functions that are "array.find" and "array.find_like" with the first being an exact match and the second being a similar match (like an SQL %LIKE% search).

These can be used in much the same way as the previous array functions:

search[0]=$( array.find "array_c" "value_c1" )
search[1]=$( array.find "array_c" "value_d1" )

search[2]=$( array.find_like "array_c" "b2" )
search[3]=$( array.find_like "array_c" "d2" )

for item in ${search[@]}; do
	(( $item == 0 )) && echo "Found" || echo "Not Found"
done

---

$ bash test.sh 
Found
Not Found
Found
Not Found

And just for clarity, this wont find anything (as it's not an exact match):

search[0]=$( array.find "array_c" "c1" )