linux - Bash 脚本 : filling up an Array inside a for loop

标签 linux bash

我尝试创建一个填充了一些值的数组。但我不知道该怎么做。我尝试了下面的方法,但没有用。

我的代码:

i=0
for c in colors; do
array[$i]=$c
echo {$c[$i]}
i=`expr $i + 1`
done

注意:“颜色”是某种返回值列表的“ps -ef”命令。例如,它有“蓝色、红色、黄色”值。

colors= 'ps -ef | grep colors'

最佳答案

您可以使用此脚本循环填充数组:

array=()
for c in $colors; do
    array+=( "$c" )
done

或者更简单:

array=( $(command) )

关于linux - Bash 脚本 : filling up an Array inside a for loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21780343/

相关文章:

linux - linux ssd + hdd上的文件访问

C程序-服务器函数错误

html - 根据 HTML 表格中的条件更改列颜色

bash - 有没有办法将命令输出的连续流发送到远程监听器

linux - 如何使用一个 ps2pdf 命令将目录中的所有 ps 文件转换为 pdf?

linux - 如何使帧缓冲区看起来像真正的显示器?

linux - cron 权限有什么问题?

regex - 如何使用 sed 从文件的第 n 行开始?

linux - 在 linux 中使用 TFS 同步文件夹

linux - 用于多个以太网端口的多个 udp 套接字