We required a script that simulates Associative arrays or Map like data structure for Shell Scripting, any body?
相关问题
- How to get the return code of a shell script in lu
- JQ: Select when attribute value exists in a bash a
- Invoking Mirth Connect CLI with Powershell script
- Emacs shell: save commit message
- bash print whole line after splitting line with if
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- Check if directory exists on remote machine with s
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
Late reply, but consider addressing the problem in this way, using the bash builtin read as illustrated within the code snippet from a ufw firewall script that follows. This approach has the advantage of using as many delimited field sets (not just 2) as are desired. We have used the | delimiter because port range specifiers may require a colon, ie 6001:6010.
Another non-bash 4 way.
You could throw an if statement for searching in there as well. if [[ $var =~ /blah/ ]]. or whatever.
For Bash 3, there is a particular case that has a nice and simple solution:
If you don't want to handle a lot of variables, or keys are simply invalid variable identifiers, and your array is guaranteed to have less than 256 items, you can abuse function return values. This solution does not require any subshell as the value is readily available as a variable, nor any iteration so that performance screams. Also it's very readable, almost like the Bash 4 version.
Here's the most basic version:
Remember, use single quotes in
case
, else it's subject to globbing. Really useful for static/frozen hashes from the start, but one could write an index generator from ahash_keys=()
array.Watch out, it defaults to the first one, so you may want to set aside zeroth element:
Caveat: the length is now incorrect.
Alternatively, if you want to keep zero-based indexing, you can reserve another index value and guard against a non-existent key, but it's less readable:
Or, to keep the length correct, offset index by one:
Example:
You can use dynamic variable names and let the variables names work like the keys of a hashmap.
For example, if you have an input file with two columns, name, credit, as the example bellow, and you want to sum the income of each user:
The command bellow will sum everything, using dynamic variables as keys, in the form of map_${person}:
To read the results:
The output will be:
Elaborating on these techniques, I'm developing on GitHub a function that works just like a HashMap Object, shell_map.
In order to create "HashMap instances" the shell_map function is able create copies of itself under different names. Each new function copy will have a different $FUNCNAME variable. $FUNCNAME then is used to create a namespace for each Map instance.
The map keys are global variables, in the form $FUNCNAME_DATA_$KEY, where $KEY is the key added to the Map. These variables are dynamic variables.
Bellow I'll put a simplified version of it so you can use as example.
Usage: