0
我有以下的小脚本:获取bash命令输出到一个数组中的shell脚本
#!/usr/bin/env bash
packages_installed=($(apt-cache -q pkgnames | tr - _ | tr . _))
echo $packages_installed
if [ ${#packages_installed[@]} -eq 0 ]; then
echo "+unable_to_find_list_of_installed_packaged"
logger -p user.error -t find_installed_packages "$(hostname -f) was unable to determine list of installed packages using apt-cache"
else
for pkg in "${packages_installed[@]}"
do
echo "+installed_package_$pkg"
done
fi
运行此然而打印:
[email protected]:~# sh test.sh
test.sh: 3: test.sh: Syntax error: "(" unexpected
这似乎表明我犯了一个错误在我生成已安装软件包列表的行上。不过,我很确定这就是你如何获得一个命令输出到数组中?
感觉就像我失去了一些愚蠢的东西。
*叹*我显然现在觉得自己像个白痴。谢了哥们。道歉为什么一定是最愚蠢的职位在这里... – Khushil
@Khushil永远不要低估其他人的愚蠢:)如果你让你感觉好一点,'bash'仍然接受一些bash-isms,即使当调用为'sh',所以它不是总是明显的是什么会起作用,什么不会。 – chepner