有没有办法从 bash 中的文本文件创建关联数组?
Is there a way to create an associative array from a text file in bash?
我目前正在创建命令列表,例如通过说 "directory install plugin-name" 我可以安装外部列表中指定的所有需要的插件。这个列表只是一个包含所有插件名称的 txt 文件。但我正在努力获取关联数组中的所有名称。
我试过这个:
while IFS=";" read line;
do " communtyList[ $line ]=1 " ;
done < community-list.txt;
所需的输出应该是
communityList[test1]=1
communityList[test2]=1
.....
它需要是一个关联数组,因为我想通过单词而不是索引来访问它。这个词将被实现为parameters/arguments。
例如 "install plugin" 而不是“1 个插件”
所以我可以这样问:
if [ ! -z "${!communtyList[]}" ];
更新,这里是完整代码:
#!/usr/bin/env bash
community(){
declare -A communtyList
while IFS= read line;
do communtyList[$line]=1 ;
done < community-list.txt;
# communtyList[test1]=1
# communtyList[test2]=1
# communtyList[test3]=1
# communtyList[test4]=1
if { [ = 'install' ] || [ = 'activate' ] || [ = 'uninstall' ] || [ = 'deactivate' ] ; } && [ ! -z ] ; then
if [ = 'all' ];
then echo " all community plugins....";
while IFS= read -r line; do echo " $line "; done < community-list.txt;
elif [ ! -z "${!communtyList[]}" ];
then echo " community plugin ''....";
else
echo -e "3[0;31m Something went wrong";
echo " Plugin '' does not exist.";
echo " Here a list of all available community plugins: ";
echo ${!communtyList[@]}
echo -e " \e[m"
fi
else
echo -e "3[0;31m Something went wrong";
if [ -z ];
then echo -e "[Plugin name] required. [community][action][plugin name] \e[m"
else
echo " Action '' does not exist.";
echo -e " Do you mean some of this? \n install \n activate \n uninstall \e[m"
fi
fi
echo ${!communtyList[@]}
}
"$@"
要使用关联数组你必须先声明它
declare -A communityList
然后你可以添加值
communityList[test1]=1
communityList[test2]=2
...
或声明
declare -A communityList=(
communityList[test1]=1
communityList[test2]=2
...
)
" communtyList[ $line ]=1 "
两边的引号表示您尝试计算第一个字符为 space 的命令。您想去掉这些引号,并可能在 "$line"
周围加上引号。
也不清楚为什么要 IFS=";"
-- 无论如何您都没有将行拆分为字段,所以这没有做任何有用的事情。输入文件中是否有分号?地点和原因;它们是什么意思?
除非您特别要求 read
在输入中用反斜杠做奇怪的事情,否则您应该更喜欢 read -r
。
最后,按照 Ivan 的建议,您必须在尝试使用数组之前将数组的类型声明为关联类型。
解决了这些问题,试试
declare -A communityList
while read -r line; do
communtyList["$line"]=1
done < community-list.txt
我目前正在创建命令列表,例如通过说 "directory install plugin-name" 我可以安装外部列表中指定的所有需要的插件。这个列表只是一个包含所有插件名称的 txt 文件。但我正在努力获取关联数组中的所有名称。
我试过这个:
while IFS=";" read line;
do " communtyList[ $line ]=1 " ;
done < community-list.txt;
所需的输出应该是
communityList[test1]=1
communityList[test2]=1
.....
它需要是一个关联数组,因为我想通过单词而不是索引来访问它。这个词将被实现为parameters/arguments。
例如 "install plugin" 而不是“1 个插件”
所以我可以这样问:
if [ ! -z "${!communtyList[]}" ];
更新,这里是完整代码:
#!/usr/bin/env bash
community(){
declare -A communtyList
while IFS= read line;
do communtyList[$line]=1 ;
done < community-list.txt;
# communtyList[test1]=1
# communtyList[test2]=1
# communtyList[test3]=1
# communtyList[test4]=1
if { [ = 'install' ] || [ = 'activate' ] || [ = 'uninstall' ] || [ = 'deactivate' ] ; } && [ ! -z ] ; then
if [ = 'all' ];
then echo " all community plugins....";
while IFS= read -r line; do echo " $line "; done < community-list.txt;
elif [ ! -z "${!communtyList[]}" ];
then echo " community plugin ''....";
else
echo -e "3[0;31m Something went wrong";
echo " Plugin '' does not exist.";
echo " Here a list of all available community plugins: ";
echo ${!communtyList[@]}
echo -e " \e[m"
fi
else
echo -e "3[0;31m Something went wrong";
if [ -z ];
then echo -e "[Plugin name] required. [community][action][plugin name] \e[m"
else
echo " Action '' does not exist.";
echo -e " Do you mean some of this? \n install \n activate \n uninstall \e[m"
fi
fi
echo ${!communtyList[@]}
}
"$@"
要使用关联数组你必须先声明它
declare -A communityList
然后你可以添加值
communityList[test1]=1
communityList[test2]=2
...
或声明
declare -A communityList=(
communityList[test1]=1
communityList[test2]=2
...
)
" communtyList[ $line ]=1 "
两边的引号表示您尝试计算第一个字符为 space 的命令。您想去掉这些引号,并可能在 "$line"
周围加上引号。
也不清楚为什么要 IFS=";"
-- 无论如何您都没有将行拆分为字段,所以这没有做任何有用的事情。输入文件中是否有分号?地点和原因;它们是什么意思?
除非您特别要求 read
在输入中用反斜杠做奇怪的事情,否则您应该更喜欢 read -r
。
最后,按照 Ivan 的建议,您必须在尝试使用数组之前将数组的类型声明为关联类型。
解决了这些问题,试试
declare -A communityList
while read -r line; do
communtyList["$line"]=1
done < community-list.txt