bash - 包含字母的增量变量

时间:2013-05-29 20:55:45

标签: bash

我有一组有效字符[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'

5 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分: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

答案 2 :(得分:1)

你可以从这开始:

 echo 0x$(( $(printf "%x" "'b'") + 1)) | xxd -r

答案 3 :(得分:1)

适用于那些希望通过执行功能来打印增量字母的人

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

alpha_increment
A
alpha_increment
B
alpha_increment
C

答案 4 :(得分:0)

我为一个项目写了这个,它使用chr和ord fucntions(也在这里找到)和一些纯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

}