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
$(…)in preference to back-quotes, all the time.nmthat print the file name on each line (usually-ror-R, IIRC). That would save you most of the manipulation work you're doing.set -vx(or simplified output) withset -x. You'll see how the variables are being evaluated, and then often very easy to fix. Also, join the 90's and stop using back-tics for command substitution.var=$(cmd_produces_output), is not 100% portable, but much easier to read and nest. Finally, only use dbl-quotes on sed cmds when you need variable expansion inside the the cmd. use single-quotes normally, and then avoid having to escape them. Good luck.arrayin a subshell, so it is not defined for the followingforloop.