0

我有以下 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}

4

1 回答 1

1

无需将零放在 cnumb 上。此外,将%03d模板用于printf

#! /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"
于 2013-05-13T10:45:27.800 回答