在bash中创建对象数组
可以在bash中创建对象数组吗 这就是我试图做到的:在bash中创建对象数组,bash,Bash,可以在bash中创建对象数组吗 这就是我试图做到的: declare -a identifications=( { email = '...', password = '...' } ) declare -a years=( '2011' '2012' '2013' '2014' '2015' '2016' ) for identification in "${identifications[@]}" do for year in "
declare -a identifications=(
{
email = '...',
password = '...'
}
)
declare -a years=(
'2011'
'2012'
'2013'
'2014'
'2015'
'2016'
)
for identification in "${identifications[@]}"
do
for year in "${years[@]}"
do
my_program --type=CNPJ --format=XLS --identification=${identification.email} --password=${identication.password} --competence=${year} --output="$identification - $year"
done
done
显然,这是行不通的,我也没有找到如何实现这一点,因为我没有找到bash对象。您可以使用(在bash 4.0中介绍)和namerefs(请参阅手册和在bash 4.3中介绍的–的第一段)来做一些小动作: 这张照片
Email: test@abc.com
Password: admin123
Email: test@xyz.org
Password: passwd1!
declare-A
声明关联数组
诀窍是分配所有以相同前缀开头的“对象”(关联数组)变量名,如identification
。${!prefix@}
符号扩展到以前缀开始的所有变量名称:
然后,为了访问关联数组的键值对,我们使用nameref属性声明for循环的控制变量:
declare -n identification
这样循环
for identification in ${!identification@}; do
使identification
的行为就像它是扩展${!identification@}
的实际变量一样
不过,很可能会更容易执行以下操作:
emails=('test@abc.com' 'test@xyz.org')
passwords=('admin123' 'passwd1!')
for (( i = 0; i < ${#emails[@]}; ++i )); do
echo "Email: ${emails[i]}"
echo "Password: ${passwords[i]}"
done
电子邮件=('test@abc.com' 'test@xyz.org')
密码=('admin123''passwd1!')
对于((i=0;i<${电子邮件[@]};++i));做
回显“电子邮件:${emails[i]}”
回显“密码:${passwords[i]}”
完成
也就是说,只需在包含信息的两个数组上循环。我倾向于使用json创建对象。对我来说,这使它非常容易和灵活
这是一个过于简单的例子
我创建了一个json文件:devices.json
{
"backup" : [{
"addr":"192.168.1.1",
"username":"backuper",
"dstfile":"firewallconfig",
"ext":".cfg",
"method":"ssh",
"rotate":"no",
"enabled":"yes"
}, {
"addr":"192.168.1.2",
"username":"backuper",
"dstfile":"routerconfig",
"ext":".cfg",
"method":"ssh",
"rotate":"no",
"enabled":"yes"
}]
}
Bash脚本:task.sh
# read the devices.json file and store it in the variable jsonlist
jsonlist=$(jq -r '.backup' "devices.json")
# inside the loop, you cant use the fuction _jq() to get values from each object.
for row in $(echo "${jsonlist}" | jq -r '.[] | @base64'); do
_jq()
{
echo ${row} | base64 --decode | jq -r ${1}
}
echo "backing up: $(_jq '.addr')"
echo "using method: $(_jq '.method')"
Done
发布原始帖子的人可以通过谷歌搜索“将json与bash一起使用”或其他方式找到。bash
只有一种数据类型:string。偶数数组只是另一种形式的语法引用,允许包含任意值(即空格)的字符串列表。(在bash
4中引入的关联数组稍好一些,但仍然不足以支持您正在寻找的数据结构类型。)ksh93
和更高版本支持您描述的定义变量。不幸的是,ksh
似乎是弃置软件,因为即使是kornshell.com上指向的手册页现在都是死链接(而且已经有一段时间了)。我无法向您指出如何将ksh用于该功能的文档。(可能就在外面的某个地方)。祝你好运。declare-n identification在bash中抛出一个错误,我查找了-n选项,但找不到任何有关它的信息?@transformerroy你有bash 4.3或更新版本吗?那是它被介绍的时候。你能把链接发到原来的帖子上吗?我在谷歌上搜索了“将json与bash结合使用”,但没有找到它。特别是我正在寻找关于函数_jq()(带下划线)的任何文档。_jq()实际上是一个独立于jq()的函数,还是我不熟悉的bash语法?我想是在这篇文章中:但他并没有解释太多_jq只是我们在for循环中创建的一个函数。一开始我觉得这个for循环有点让人困惑。但是很好。
{
"backup" : [{
"addr":"192.168.1.1",
"username":"backuper",
"dstfile":"firewallconfig",
"ext":".cfg",
"method":"ssh",
"rotate":"no",
"enabled":"yes"
}, {
"addr":"192.168.1.2",
"username":"backuper",
"dstfile":"routerconfig",
"ext":".cfg",
"method":"ssh",
"rotate":"no",
"enabled":"yes"
}]
}
# read the devices.json file and store it in the variable jsonlist
jsonlist=$(jq -r '.backup' "devices.json")
# inside the loop, you cant use the fuction _jq() to get values from each object.
for row in $(echo "${jsonlist}" | jq -r '.[] | @base64'); do
_jq()
{
echo ${row} | base64 --decode | jq -r ${1}
}
echo "backing up: $(_jq '.addr')"
echo "using method: $(_jq '.method')"
Done