How to define hash tables in bash?
来源:互联网 发布:glf软件下载 编辑:程序博客网 时间:2024/06/05 15:54
Bash 4
Bash 4 natively supports this feature. Make sure your script's hashbang is #!/usr/bin/env bash
or#!/bin/bash
or anything else that references bash
and not sh
. Make sure you're executing your script, and not doing something silly like sh script
which would cause your bash
hashbang 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 eval
to 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", declare
is a far safer option. It does not evaluate data as bash code like eval
does, 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: declare
cannot be put in a function. Any use of declare
inside 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 awk
before doing ugly hacks as described above. And definitely stay the heck away from eval
hackery.
http://stackoverflow.com/questions/1494178/how-to-define-hash-tables-in-bash
- How to define hash tables in bash?
- linux, how to define a new array in bash
- How to define a bash function for use in any script?
- How to define an array in c#.
- Emacs: How to Define Templates in YASnippet
- How to define alias in Linux
- how to output quotes in bash prompt
- How to answer 'yes' in bash Script
- How to Create Tables
- Javascript: hash tables in javascript
- Javascript: hash tables in javascript
- How to define a custom separator in TLex
- How to define multiple controllers for one view in angularJS?
- How to Enable the Linux Bash Shell in Windows 10
- How to sum values in an array with different hash
- java How to Use Tables
- How to define user menu ?
- How to create simple and advanced pivot tables in C# and ASP.NET
- virtualBox 里安装功能增强组件
- 做人才网站遇到的问题--更新中
- Qt Creator编译时:cannot open file 'debug\QtGuiEx.exe' File not found
- wikioi 1214 线段覆盖
- 面向对象和面向过程的区别
- How to define hash tables in bash?
- Remove
- So Beautiful 旅程 ——HTML5
- .NET中TextBox控件设置ReadOnly=true后台取不到值三种解决方法
- 常用正则
- zip 的压缩原理与实现
- IT技术如何转向销售创业
- 我的shiro之旅: 五 shiro与普通web项目集成
- scrollview的代理方法