读命令的输出转换成在bash阵列 [英] reading output of command into array in bash

查看:83
本文介绍了读命令的输出转换成在bash阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的IAM在bash和我需要的命令在我的脚本输出读入的script.The命令的阵列例如:

Iam new at bash and I need to read the output of the command in my script into an array in the script.The command is for example

ps aux | grep | grep | x 

和它给出了这样一行输出线;

and it gives the output line by line like this;

10
20
30

我需要阅读从命令输出数组中的值,然后我会做一些工作,如果阵列&LT的大小; 3

I need to read the values from the command output to array and then i will do some work if the size of array<3

推荐答案

您可以使用

my_array=( $(<command>) )

要命令&LT的输出存储;命令方式&gt; 入阵 my_array

您可以通过访问该数组的长度

You can access the length of that array using

my_array_length=${#my_array[@]}

现在的长度存储在 my_array_length

这篇关于读命令的输出转换成在bash阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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