如何从命令输出的行创建数组 [英] How to create an array from the lines of a command's output
本文介绍了如何从命令输出的行创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为failedfiles.txt
的文件,其内容如下:
I have a file called failedfiles.txt
with the following content:
failed1
failed2
failed3
我需要使用grep返回该文件中每一行的内容,并将输出保存在要访问的列表中.所以我想要这样的东西:
I need to use grep to return the content on each line in that file, and save the output in a list to be accessed. So I want something like this:
temp_list=$(grep "[a-z]" failedfiles.txt)
但是,这是我输入
echo ${temp_list[0]}
我得到以下输出:
failed1 failed2 failed3
但是我想要的是什么时候去做
But what I want is when I do:
echo ${temp_list[0]}
要打印
failed1
当我这样做时:
echo ${temp_list[1]}
要打印
failed2
谢谢.
推荐答案
您未创建了数组.您所做的是命令替换,只需将将命令输出到变量.
You did not create an array. What you did was Command Substitution which would simply put the output of a command into a variable.
要创建数组,请说:
temp_list=( $(grep "[a-z]" failedfiles.txt) )
您可能还想参考关于阵列的指南.
这篇关于如何从命令输出的行创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文