2013-12-18 73 views
0

舉例來說,如果我有這樣的數組:是否可以在bash中爲稀疏數組設置默認值?

SourceFolder[0]="inbound1" 
SourceFolder[1]="inbound2" 
SourceFolder[2]="inbound3" 
SourceFolder[100]="inbound100" 

而且我通過它去:

for i in "${!SourceFolder[@]}" 
do 
echo "SourceFolder: " ${SourceFolder[$i]} 
done 

是否有可能使其返回一些默認值未初始化索引(3-99 )?

所以,從3到99指標將返回,例如, 「defaultFolder」

回答

1
#!/usr/bin/env bash 

for i in {0..100}; do 
    SourceFolder["${i}"]="defaultFolder" 
done 

SourceFolder[0]="inbound1" 
SourceFolder[1]="inbound2" 
SourceFolder[2]="inbound3" 
SourceFolder[100]="inbound100" 

for i in "${!SourceFolder[@]}"; do 
    echo "SourceFolder: ${SourceFolder[$i]}" 
done 
相關問題