Dizinde bir diziye eriştiğimde, bash, dizimde başka bir bash betiğinin kaynağında içe aktarılan bir değişkense garip davranış alıyorum . Bu davranışa ne sebep olur? Başka bir bash komut dosyasından alınan bir dizi, çalışan komut dosyasında tanımlanan bir dizi ile aynı şekilde davranması için nasıl düzeltilebilir?Dizideki Dizine Göre Dizine Erişim diziden kaynaklanmış bir dosyadan doğru çalışmaz
$ {sayılar [0]} "bir ila iki üç" ve değil evals "bir" tam testini should.The olarak bu davranış sergilemeye çalıştık aşağıda gösterilmiştir: Testin
Kaynak. sh: number.sh ait
#!/bin/bash
function test {
echo "Length of array:"
echo ${#numbers[@]}
echo "Directly accessing array by index:"
echo ${numbers[0]}
echo ${numbers[1]}
echo ${numbers[2]}
echo "Accessing array by for in loop:"
for number in ${numbers[@]}
do
echo $number
done
echo "Accessing array by for loop with counter:"
for ((i = 0 ; i < ${#numbers[@]} ; i=$i+1));
do
echo $i
echo ${numbers[${i}]}
done
}
numbers=(one two three)
echo "Start test with array from within file:"
test
source numbers.sh
numbers=${sourced_numbers[@]}
echo -e "\nStart test with array from source file:"
test
Kaynak: test.sh ait
#!/bin/bash
#Numbers
sourced_numbers=(one two three)
Çıktı:
Start test with array from within file:
Length of array:
3
Directly accessing array by index:
one
two
three
Accessing array by for in loop:
one
two
three
Accessing array by for loop with counter:
0
one
1
two
2
three
Start test with array from source file:
Length of array:
3
Directly accessing array by index:
one two three
two
three
Accessing array by for in loop:
one
two
three
two
three
Accessing array by for loop with counter:
0
one two three
1
two
2
three
Kabuk hata ayıklaması ile 'set -vx' size bir şey gösterecektir. (Şu anda deneme yapmak için zamanınız yok). İyi şanslar. – shellter