如何初始化输出一个bash数组由另一命令? [英] How to initialize a bash array with output piped from another command?

查看:144
本文介绍了如何初始化输出一个bash数组由另一命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正是有管道,其中列出一串数字(在一个单独的行每​​个号码)命令的输出,并初始化这些数字一个bash数组?

Is there anyway to pipe the output of a command which lists a bunch of numbers (each number in a separate line) and initialize a bash array with those numbers?

详细信息:
这列出了已在以下日期范围已提交3变更号码。然后将输出通过管道输送到一刀切,以进一步筛选它得到只是变更号码。

Details: This lists 3 changelist numbers which have been submitted in the following date range. The output is then piped to "cut" to filter it further to get just the changelist numbers.

P4改变-m 3 -u EDK -s提交@ 2009/05/01 @现在|切-d''-f 2

例如。

422311

543210

444000

这怎么可能保存这份名单在bash数组?

How is it possible to store this list in a bash array?

推荐答案

您可以在蜱执行命令,并设定像阵列,

You can execute the command under ticks and set the Array like,

ARRAY=(`command`)

另外,你可以在命令的输出保存到一个文件,它的猫同样,

Alternatively, you can save the output of the command to a file and cat it similarly,

command > file.txt
ARRAY=(`cat file.txt`)

或者,只需在下面的意见建议下列形式之一,

Or, simply one of the following forms suggested in the comments below,

ARRAY=(`< file.txt`)
ARRAY=($(<file.txt))

这篇关于如何初始化输出一个bash数组由另一命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆