hugot / bash-array-utils Goto Github PK
View Code? Open in Web Editor NEWUtility functions for common array operations in bash
License: MIT License
Utility functions for common array operations in bash
License: MIT License
I see that you use this check in hasValue
if ! Array::isValid "$1"; then
echo "$(caller): $1 is not a valid array."
exit 1
fi
I think it would be better to return instead of exit.
My use-case is the following:
# source array functions
if Array::hasValue my_array my_value; then
: # do something
else
: # do something else
fi
: # do other things
...in this case the script would simply exit, not executing the else clause or the rest of the code. I think the choice of exiting should be left to the user (with set -e
or other ways).
Another way would be to offer two modes: a strict mode where the array is required to be set, and a non-strict mode where is does not matter.
By the way, you use Array::isValid "$1" || return $?
in all other places, so maybe it was not intended to use exit?
Hello!
Would you be interested in offering a function that can return the index of a given element?
I was thinking something like Array::indexOf
, taking an array as $1
and the element as $2
, returning the index between 0 and n, or -1 if not found.
About the way to return it, we could either write to stdout, but it does not feel very efficient because we would then have to use a subprocess like index=$(Array::indexOf array elem)
. So maybe we would instead allow for a third parameter as the name of the variable to set with the result: Array::indexOf array elem index
.
I can open a PR if you're interested! ๐
I believe people usually go with the (not so good) name of "array" in shell scripts (maybe I'm wrong), so why not reserve the name "_array" instead? Same for "_variable" instead of "variable", though this one seems less important.
$ [[ 09 == +([0-9]) ]] && echo true
true # will trigger numeric comparison with -eq, but:
$ [[ 09 -eq 09 ]] && echo true
bash: 09: value too great for base (error token is "09")
There might be other cases where the string is composed only of numbers, but the user actually want to compare it as a string.
A solution to keep this feature would be to check if the array was declared with the -i
option:
$ declare -ia int_array
$ declare -p int_array
declare -ai int_array
$ if declare -p int_array | grep -q '^declare -ai'; then echo true; fi
true
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.