[英]Debian output from installed packages in bash array
Sorry for maybe a noob question but I am pretty new to Linux. 抱歉,可能有一个菜鸟问题,但是我对Linux很陌生。 I am trying to get all output from following command in an array and loop through it (in bash).
我试图从数组中的以下命令获取所有输出,并通过它循环(在bash中)。
packages=$(dpkg-query --show --showformat='${Package}' )
for p in "${packages[@]}"; do
echo "Package: ${p}"
done
I don't know what I am doing wrong, but maybe someone can help me out. 我不知道我在做什么错,但也许有人可以帮我。
Thanks in advance. 提前致谢。
Greetings MC 问候MC
Thanks for you help. 感谢您的帮助。 my solution now is this:
我现在的解决方案是这样的:
while IFS= read -r line; do
echo "${line}"
done <<< "$(dpkg-query --show --showformat='${Package}\n')"
A number of things. 有很多东西。
First 第一
packages=$(dpkg-query --show --showformat='${Package};'
doesn't create an array. 不创建数组。
It creates a string. 它创建一个字符串。 You need
()
around the command to split it into an array. 您需要在命令周围加上
()
才能将其拆分为一个数组。
Second: 第二:
Even if you were using an array there, you wouldn't want to do this this way. 即使在那里使用数组,您也不想这样做。
See Bash FAQ 001 for appropriate ways to read data from a file/command/etc. 有关从文件/命令/等读取数据的适当方法,请参见Bash FAQ 001 。 line-by-line.
逐行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.