我有一个日期值和一个关联的整数,我希望通过我的bash脚本存储在一个数组中。目前我只是把违规行放在我认为问题是本地化的。
chartDate=2014-12-01;
dayTotal=5;
dataArray[${chartDate}]=$dayTotal;
尝试以上操作时,我得到:
line 125: 2014-12-01: value too great for base (error token is "01")
如果我尝试以下任一方法,我会遇到不同的错误:
dataArray["${chartDate}"]=$dayTotal;
dataArray[\""${chartDate}"\"]=$dayTotal;
有什么建议吗?
完整的脚本是:
function getChartData () {
chartDate=$(chartStartsAt); #$(chartStartAt) returns a date formatted %Y-%m-%d
echo $chartDate;
now=`date +%Y-%m-%d`;
echo $now;
issueCount=`jq '. | length' output/json/FullIssueList.json`; #returns an integer
declare -a dataArray;
while [ "${chartDate}" \< "${now}" ]
do
#increment chartDate by one day
chartDate=$(date -j -v +1d -f "%Y-%m-%d" "${chartDate}" +%Y-%m-%d);
echo $chartDate
#Get number of records that match that date
dayTotal=`grep -c "${chartDate}" output/json/FullIssueList.json`;
echo $dayTotal;
#Put values in the array
dataArray[${chartDate}]=$dayTotal;
done
for K in "${!dataArray[@]}"; do echo $K --- ${MYMAP[$K]}; done
}
这里是评论中建议的-uxv输出,请注意以上是另外7或8个结果:
+ set -uxv
+ dataArray[${chartDate}]=0
+ '[' 2014-12-06 '<' 2015-01-09 ']'
date -j -v +1d -f "%Y-%m-%d" "${chartDate}" +%Y-%m-%d
++ date -j -v +1d -f %Y-%m-%d 2014-12-06 +%Y-%m-%d
+ chartDate=2014-12-07
+ echo 2014-12-07
2014-12-07
grep -c "${chartDate}" output/json/FullIssueList.json
++ grep -c 2014-12-07 output/json/FullIssueList.json
+ dayTotal=0
+ echo 0
0
+ set -uxv
+ dataArray[${chartDate}]=0
+ '[' 2014-12-07 '<' 2015-01-09 ']'
date -j -v +1d -f "%Y-%m-%d" "${chartDate}" +%Y-%m-%d
++ date -j -v +1d -f %Y-%m-%d 2014-12-07 +%Y-%m-%d
+ chartDate=2014-12-08
+ echo 2014-12-08
2014-12-08
grep -c "${chartDate}" output/json/FullIssueList.json
++ grep -c 2014-12-08 output/json/FullIssueList.json
+ dayTotal=0
+ echo 0
0
+ set -uxv
+ dataArray[${chartDate}]=0
./GetJiraReleaseIssues.sh: line 125: 2014-12-08: value too great for base (error token is "08")
答案 0 :(得分:2)
要使用关联数组,必须先将其声明为
declare -A dataArray
# ^
# CAPITAL A
如果不这样做,bash会将数组视为普通数组,并尝试将索引解释为数值表达式。