是否有可能设置在bash稀疏数组的默认值? [英] Is it possible to set a default value for sparse array in bash?

查看:112
本文介绍了是否有可能设置在bash稀疏数组的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有这样的数组:

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

和我通过它:

 为我在$ {!SourceFolder [@]}

 回声SourceFolder:$ {SourceFolder [$ i]}
DONE

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

因此​​,从3到99的索引它将返回,例如,defaultFolder


解决方案

 #!的/ usr / bin中/ env的庆典因为我在{0..100};做
    SourceFolder [$ {I}] =defaultFolder
DONESourceFolder [0] =inbound1
SourceFolder [1] =inbound2
SourceFolder [2] =inbound3
SourceFolder [100] =inbound100因为我在$ {SourceFolder [@]!};做
    回声SourceFolder:$ {SourceFolder [$ i]}
DONE

for example, if I have this array:

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

And I go through it:

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

Is it possible to make it to return some default value for not initialized indexed (3-99)?

so, for indexes from 3 to 99 it will return, for instance, "defaultFolder"

解决方案

#!/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

这篇关于是否有可能设置在bash稀疏数组的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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