我有以下 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 个字符的 block 替换为另一个 3 个字符的 block 。如何用 bash 做到这一点
是否可以用代码替换子字符串${cnumb:i:i+3}
?
最佳答案
无需将零放入 cnumb。此外,为 printf
使用 %03d
模板:
#! /bin/bash
pass="kall"
cnumb=''
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+=$code
done
echo "$cnumb"
关于linux - 用 bash 中的另一个子字符串替换一个子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16520140/