无法元素在bash添加到阵列 [英] Unable to add element to array in bash
问题描述
我有以下问题。 Let's假设 $ @
包含唯一有效的文件。变量文件
包含当前文件(目前我的文件)的名称。然后变量元素
包含格式为文件中的数据:功能
现在,当变量元素不是空的,应该投入的阵列。这就是问题所在。如果我赞同元素
,它包含了我想要的东西,虽然它并不存储在阵列中,因此对于周期不会打印出任何东西。
我已经写了两个方法我尝试插入元素到数组,但既不工程。你能告诉我,什么我做错了吗?
我使用的是Linux Mint的16。
#!/斌/庆典纳米$ @ |而读线
做
模式=`回声\\$线\\| sed的-n \\S / ^ \\:$ / \\ 1 / P \\`(* \\)。
如果[-n$模式];然后
文件=$模式
科幻
元素=`回声\\$线\\| sed的-n \\S / ^ U \\([0-9A-ZA-Z _] * \\)* / $文件:\\ 1 / P \\`
如果[-n$元素];然后
阵列+ =($元件)
#array [$ [$ {#数组[@]} + 1] =$元素
回声元素 - $元素
科幻
DONE在$ {数组[@]}j
做
回声$ J
DONE
您的问题是,,而
循环在子shell中运行,因为它是第二个命令在管道,所以在这个循环所做的任何更改不可循环退出后。
您有几种选择。我经常使用 {
和}
的的命令分组:
纳米$ @|
{
而读线
做
...
DONE
在$ {数组[@]}j
做
回声$ J
DONE
}
在庆典
,你也可以使用的进程替换:
而读线
做
...
完成< ≤(纳米$ @)
此外,最好使用 $(...)
代替反引号`...`的
(并不仅仅是因为它是艰苦的工作取回报价到降价的文字!)。
您行:
元素=`回声\\$线\\| sed的-n \\S / ^ U \\([0-9A-ZA-Z _] * \\)* / $文件:\\ 1 / p \\`
可以写成:
元素=$(回声$线。| sed的-nS / ^ U \\([0-9A-ZA-Z _] * \\)* / $文件:\\ 1 / p)
甚至是:
元素= $(回声$线| sed的-nS / ^ U \\([0-9A-ZA-Z _] * \\)* / $文件: \\ 1 / p)
当你需要他们嵌套它确实有帮助。例如,要列出毗邻 LIB
目录,其中 GCC
发现:
的ls -l $(目录名称$(目录名称$(其中GCC)))/ lib目录
VS
的ls -l`目录名\\目录名``\\\\\\其中GCC \\\\\\`\\``/ lib目录
我知道,我觉得这更容易!
I have the following problem. Let´s assume that $@
contains only valid files. Variable file
contains the name of the current file (the file I'm currently "on"). Then variable element
contains data in the format file:function
.
Now, when variable element is not empty, it should be put into the array. And that's the problem. If I echo element
, it contains exactly what I want, although it is not stored in array, so for cycle doesn't print out anything.
I have written two ways I try to insert element into array, but neither works. Can you tell me, What am I doing wrong, please?
I'm using Linux Mint 16.
#!/bin/bash
nm $@ | while read line
do
pattern="`echo \"$line\" | sed -n \"s/^\(.*\):$/\1/p\"`"
if [ -n "$pattern" ]; then
file="$pattern"
fi
element="`echo \"$line\" | sed -n \"s/^U \([0-9a-zA-Z_]*\).*/$file:\1/p\"`"
if [ -n "$element" ]; then
array+=("$element")
#array[$[${#array[@]}+1]]="$element"
echo element - "$element"
fi
done
for j in "${array[@]}"
do
echo "$j"
done
Your problem is that the while
loop runs in a subshell because it is the second command in a pipeline, so any changes made in that loop are not available after the loop exits.
You have a few options. I often use {
and }
for command grouping:
nm "$@" |
{
while read line
do
…
done
for j in "${array[@]}"
do
echo "$j"
done
}
In bash
, you can also use process substitution:
while read line
do
…
done < <(nm "$@")
Also, it is better to use $(…)
in place of back-quotes `…`
(and not just because it is hard work getting back quotes into markdown text!).
Your line:
element="`echo \"$line\" | sed -n \"s/^U \([0-9a-zA-Z_]*\).*/$file:\1/p\"`"
could be written:
element="$(echo "$line" | sed -n "s/^U \([0-9a-zA-Z_]*\).*/$file:\1/p")"
or even:
element=$(echo "$line" | sed -n "s/^U \([0-9a-zA-Z_]*\).*/$file:\1/p")
It really helps when you need them nested. For example, to list the lib
directory adjacent to where gcc
is found:
ls -l $(dirname $(dirname $(which gcc)))/lib
vs
ls -l `dirname \`dirname \\\`which gcc\\\`\``/lib
I know which I find easier!
这篇关于无法元素在bash添加到阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!