简体   繁体   English

使用bash在Linux中制作数组

[英]Making an array in Linux, using bash

Assume that the Pid of active processes on my machines are 1000 and 2000. I am trying to make an array in Linux such that 假设我的机器上活动进程的Pid是1000和2000。我正在尝试在Linux中创建一个数组,这样

The command echo ${Pid_Current[0]} gives 1000 in output 命令echo ${Pid_Current[0]}给出1000的输出

The command echo ${Pid_Current[1]} gives 2000 in output 命令echo ${Pid_Current[1]}输出2000

Here is my code: 这是我的代码:

declare -a Pid_Current 

Pid_Current=$(ps -aF | tail -n +2 | awk '{print $2}')

However, instead of the desired output I explained above, I receive the following output: 但是,我收到以下输出,而不是上面解释的所需输出:

echo ${Pid_Current[0]} gives 1000 2000 in output echo ${Pid_Current[0]}给出1000 2000的输出

echo ${Pid_Current[1]} gives nothing in output echo ${Pid_Current[1]}在输出中什么也没有

Would you please advise me what part of my code is incorrect? 您能告诉我代码的哪一部分不正确吗?

In bash array assignment is done by enclosing the expression in parenthesis, so to use array assignment you need to write: 在bash中,数组分配是通过将表达式括在括号中完成的,因此要使用数组分配,您需要编写:

Pid_Current=($(ps -aF | tail -n +2 | awk '{print $2}'))

Without parenthesis the result of the expression is assigned to Pid_Current[0] 不带括号,表达式的结果将分配给Pid_Current[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM