向bash数组添加值 [英] Adding values to bash arrays

查看:57
本文介绍了向bash数组添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码的期望是,我将拥有两个共享内存ID数组.列出ID的附件为零的一个,列出ID的附件为零以上的一个.

My expectation of the following code is that I'll have two arrays of shared memory ID's. One that lists the ID's with zero attachments, and one that lists the ID's with more than zero attachments.

我正在解析的命令输出是:

The command ouput that I'm parsing is:

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x6c020df2 131072     monitor    600        4276656    6                       
0x77020d34 4620289    usera      666        104        0                       
0x78020d34 4653058    usera      666        651328000  0                       
0x72020d34 4685827    usera      666        15808      0                       

代码如下:

free_shmids=()
used_shmids=()

IFS=$'\n'

for line in $(sudo ipcs -m | grep '0x'); do
  nattch=$(echo $line | awk '{ print $NF; }')
  shmid=$(echo $line | awk '{ print $2; }')

  if [ $nattch -eq "0" ]; then
    free_shmids+=($shmid)
  else
    used_shmids+=($shmid)
  fi
done

unset -v IFS

相反,免费ID的列表只是三个可用ID中的第一个免费ID.我在这里只有一个用过的共享内存段,因此即使我无法证明它,也以同样的方式失败.

Instead, the list of free ID's is just the first free ID of the three available. I've only got one used shared memory segment here, so I assume that's failing the same way even though I can't prove it.

作为参考,我正在运行bash 4.3.11,因此,我很确定自己可以访问数组的 + = 语法.

For reference, I'm running bash 4.3.11, so I'm pretty sure I've got access to the += syntax for arrays.

要迭代空闲ID的数组,我正在这样做:

To iterate the array of free ID's, I'm doing this:

for id in $free_shmids; do
  echo $id
done

我当然打算在for循环的主体中做一些更复杂的事情,但是这种精确的迭代方法不会列出数组的每个元素.

I intend to do something more complex in the body of the for loop, of course, but that exact method of iteration doesn't list every element of the array.

推荐答案

我敢打赌,您正在这样做:

I bet you're doing this:

echo $free_shmid

由于这是一个数组,因此您使用的语法不正确.在不指定索引的情况下取消引用数组只会给您数组中的第一个元素.做这样的事情:

Since that is an array, you're not using the right syntax. De-referencing an array without specifying an index will only give you the first element in the array. Do something like this:

    printf "free: %s\n" "${free_shmid[@]}"
    printf "used: %s\n" "${used_shmid[@]}"

与使用 for 循环读取输入行相比,通常首选使用 while读取循环.例如

And using a while read loop is generally preferred than using a for loop to read lines of input. For example

#!/bin/bash
data="------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x6c020df2 131072     monitor    600        4276656    6                       
0x77020d34 4620289    usera      666        104        0                       
0x78020d34 4653058    usera      666        651328000  0                       
0x72020d34 4685827    usera      666        15808      0                       "

echo "$data" | {
    read title       # first line
    read header      # second line
    # now read and process the rest
    while read -r key shmid owner perms bytes nattch status; do
        if ((nattch == 0)); then
            free_shmid+=($shmid)
        else
            used_shmid+=($shmid)
        fi
    done

    printf "free: %s\n" "${free_shmid[@]}"
    printf "used: %s\n" "${used_shmid[@]}"
}

输出

free: 4620289
free: 4653058
free: 4685827
used: 131072


要遍历数组的元素,您必须必须执行以下操作:

for elem in "${array[@]}"; do ...

是的,所有引号和其他字符都是绝对必需的.

Yes, all the quotes and other characters are absolutely required.

这篇关于向bash数组添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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