我有以下bash脚本
pass="kall"
cnumb="000000000000"
for (( i=0; i<${#pass}; i++))
do
code=`printf '%03d' "'${pass:i:i+1}"` #generate the code ASCII of letter as string with 3 chars
cnumb = .... #put the code ASCII of "k" in the first bloc of 3 chars , put the code ASCII of "a" in the second bloc of 3 chars, ...
done
正如代码中所描述的那样,我希望在循环中的每个迭代中重新演绎cnumb中由3个字符串组成的3个字符集。如何使用bash
是否可以用代码替换子字符串${cnumb:i:i+3}
?
答案 0 :(得分:1)
无需将数字加零。另外,使用%03d
的{{1}}模板:
printf