9

我有一组有效字符 [0-9a-z_] 和一个分配了这些字符之一的变量。我想要做的是能够将该变量增加到集合中的下一个。

如果需要,我可以处理从“9”增加到“a”和“z”到“_”的“特殊”情况,但我不知道如何增加字母。

#!/bin/bash
y=b
echo $y  # this shows 'b'
y=$((y+1))
echo $y  # this shows '1', but I want it to be 'c'
4

5 回答 5

13
y=b
echo "$y"  # this shows 'b'
y=$(echo "$y" | tr "0-9a-z" "1-9a-z_")
echo "$y"  # this shows 'c'

请注意,这不处理 $y = "_" 的情况(不确定你想要什么,无论如何它可能需要单独处理),如果 $y 的长度超过一个字符,它会“增加”所有这些(即“10”->“21”、“09g”->“1ah”等)。

于 2013-05-29T22:01:59.530 回答
3

也许这可以是一个解决方案:

a=({0..9} {a..z} _)
echo ${a[*]}
yc=11
echo ${a[yc]}
((++yc))
echo ${a[yc]}
echo ${a[++yc]}

#Alternative 
declare -A h
# Fill the has point to the next character
for((i=0;((i+1))<${#a[*]};++i)) { h[${a[i]}]=${a[i+1]};}
y=b
echo $y, ${h[$y]}

输出:

0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z _
b
c
d
b, c
于 2013-05-29T21:10:03.477 回答
2

对于那些想通过执行函数来打印递增字母的人

ALPHA=( {A..Z} ) 
alpha_increment () { echo ${ALPHA[${i:-0}]}; ((i++)) ;}

alpha_increment
A
alpha_increment
B
alpha_increment
C
于 2019-08-15T13:50:34.963 回答
1

你可以从这个开始:

 echo 0x$(( $(printf "%x" "'b'") + 1)) | xxd -r
于 2013-05-29T21:10:58.747 回答
0

我为一个项目编写了这个,它使用了 chr 和 ord 函数(也可以在这里找到)和一些纯 bash(函数中只有外部调用的是 tr),如果你做的字符超过 100 个,我会使用其他的,但是对于我测试中的短字符串,它实际上比 python 快一点。此脚本也将任何输入小写,您必须将其修改为大写。将这些函数放入脚本中(或剪切并粘贴到 shell 中)后,您可以这样做

inc_string abz9z9

然后回来。

aca0a0

chr() {
  [ "$1" -lt 256 ] || return 1
  printf "\\$(printf '%03o' "$1")"
}

ord() {
  LC_CTYPE=C printf '%d' "'$1"
}

inc_string () 
{
string="$1";
lcstring=$(echo $string | tr '[:upper:]' '[:lower:]');


for ((position=$((${#lcstring}-1));position>=0;position--));do  

  if [ "${lcstring:position:1}" = 'z' ]; then
    if [ "$position" -eq "$((${#lcstring}-1))" ]; then
       newstring="${lcstring:0:$(($position))}a";
       lcstring="$newstring";
    elif [ "$position" -eq "0" ]; then 
       newstring="a${lcstring:$((position+1))}";
       echo $newstring;
       break;
    else
       newstring="${lcstring:0:$(($position))}a${lcstring:$((position+1))}";
       lcstring="$newstring";
    fi
  elif [ "${lcstring:position:1}" = '9' ]; then
    if [ "$position" -eq "$((${#lcstring}-1))" ]; then
       newstring="${lcstring:0:$(($position))}0";
       lcstring="$newstring";
    elif [ "$position" -eq "0" ]; then  
       newstring="0${lcstring:$((position+1))}";
       echo $newstring;
       break;
    else
       newstring="${lcstring:0:$(($position))}0${lcstring:$((position+1))}";
       lcstring="$newstring";
    fi  
  else
    if [ "$position" -eq "$((${#lcstring}-1))" ]; then
       newstring="${lcstring:0:$(($position))}$(chr $(($(ord ${lcstring:position})+1)))";
       echo $newstring;
       break;
    elif [ "$position" -eq "0" ]; then
       newstring="$(chr $(($(ord ${lcstring:position})+1)))${lcstring:$((position+1))}";
       echo $newstring;
       break;
    else
       newstring="${lcstring:0:$(($position))}$(chr $(($(ord ${lcstring:position})+1)))${lcstring:$(($position+1))}";
       echo $newstring;
       break;               
    fi
  fi
done

}
于 2015-11-13T18:18:45.020 回答