How to define hash tables in bash?
2013-09-26 14:32
302 查看
Bash 4
Bash 4 natively supports this feature. Make sure your script's hashbang is #!/usr/bin/env bashor
#!/bin/bashor
anything else that references
bashand
not
sh.
Make sure you're executing your script, and not doing something silly like
sh scriptwhich would cause your
bashhashbang
to be ignored. This is basic stuff, but so many keep failing at it, hence the re-iteration.
You declare an associative array by doing:
declare -A animals
You can fill it up with elements using the normal array assignment operator:
animals=( ["moo"]="cow" )
Or merge them:
declare -A animals=( ["moo"]="cow" )
Then use them just like normal arrays.
"${animals[@]}"expands
the values,
"${!animals[@]}"(notice
the
!)
expands the keys. Don't forget to quote them:
echo "${animals["moo"]}" for sound in "${!animals[@]}"; do echo "$sound - ${animals["$sound"]}"; done
Bash 3
Before bash 4, you don't have associative arrays. Do not use evalto
emulate them. You must avoid eval like the plague, because it is the plague of shell scripting. The most important reason is that you don't
want to treat your data as executable code (there are many other reasons too).
First and foremost: Just consider upgrading to bash 4. Seriously. The
future is now, stop living in the past and suffering from it by forcing stupid broken and ugly hacks on your code and every poor soul stuck
maintaining it.
If you have some silly excuse why you "can't upgrade",
declareis
a far safer option. It does not evaluate data as bash code like
evaldoes,
and as such it does not allow arbitrary code injection quite so easily.
Let's prepare the answer by introducing the concepts:
First, indirection (seriously; never use this unless you're mentally ill or have some other bad excuse for writing hacks).
$ animals_moo=cow; sound=moo; i="animals_$sound"; echo "${!i}" cow
Secondly,
declare:
$ sound=moo; animal=cow; declare "animals_$sound=$animal"; echo "$animals_moo" cow
Bring them together:
# Set a value: declare "array_$index=$value" # Get a value: arrayGet() { local array=$1 index=$2 local i="${array}_$index" printf '%s' "${!i}" }
Let's use it:
$ sound=moo $ animal=cow $ declare "animals_$sound=$animal" $ arrayGet animals "$sound" cow
Note:
declarecannot
be put in a function. Any use of
declareinside
a bash function turns the variable it creates local to the scope of that function, meaning we can't access or modify global arrays with it. (In
bash 4 you can use declare -g to declare global variables - but in bash 4, you should be using associative arrays in the first place, not this hack.)
Summary
Upgrade to bash 4 and use declare -A. If you can't, consider switching entirely to
awkbefore
doing ugly hacks as described above. And definitely stay the heck away from
evalhackery.
http://stackoverflow.com/questions/1494178/how-to-define-hash-tables-in-bash
相关文章推荐
- How to define a bash function for use in any script?
- linux, how to define a new array in bash
- how to get the space size of some tables in one database?
- How to create zebra stripes in JSP tables
- How to define an array in c#.
- how to output quotes in bash prompt
- [Groovy] How to check if element in groovy array/hash/collection/list?
- how to output quotes in bash prompt
- how to get the space size of some tables in one database?
- How to Switch the Driving Table in a Hash Join (文档 ID 171940.1)
- How to Use the BASH "for" Loop in Shell Scripts
- how to merge array of hash based same keys in ruby?
- R Markdown: How to format tables and figures in .docx files
- How to define multiple controllers for one view in angularJS?
- Weblogic Server in the Chinese show how to solve the problem hash
- How to calculate the MD5 hash of a large file in C?
- How to define alias in Linux
- grails - How to check if element in groovy array/hash/collection/list? - Stack Overflow
- How to reduce number of records on CTXSYS.DR$WAITING,CTXSYS.DR$PENDING tables in Oracle EBS?
- HowTo hide Password in shell Scripting using Bash