我需要创建一个计数器以在 shell(bash)脚本中使用,每次调用脚本时,包含的数字必须增加一,并且数字应保持为六位数字,因此初始值为 000000,然后是 000001,然后是 000002 等等.....我正在做的是,我创建了一个名为“counter”的文件,该文件在第一行包含一个 6 位整数。所以从脚本我有这个代码:
index= cat /path/counter | tail -1 #get the counter
tmp=`expr $index + 1` #clone and increase the counter
echo "" >/path/counter #empty the counter
echo ${tmp} >/path/counter #insert clone
问题是它在第二步不起作用,可能是第一步实际上失败了,你有什么建议吗?
一个选项如下:
#!/bin/bash
read index < /path/counter
declare -i tmp=index+1
printf "%06d" $tmp > /path/counter
问题是它只将文件的内容提升到 000007,之后我得到:
-bash: 000008: value too great for base (error token is "000008")
有什么建议吗?