2013-05-29 19 views
6

私は有効な文字セット[0-9a-z_]とこれらの文字の1つに割り当てられた変数を持っています。私がしたいのは、その変数をセット内の次のものに増やすことができることです。bash - 文字を含むインクリメント変数

「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' 
+0

aca0a0

あなたはこれを探していますか? http://stackoverflow.com/a/13128083/297323 –

答えて

6
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"など)。

+0

これは私が探していたものです。私は、$ yが1文字以上の特殊な場合や、 "_"と等しい場合に対処できます。これは私が使用した醜い配列よりはるかに良いソリューションです。ありがとう! –

+0

'y = b; y = $(tr "0-9a0-z" "1-9a-z_" <<< $ y); echo $ y; ' – dylnmc

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 
+1

これは機能し、実際に私が自分で構築したものにかなり近いですが、最も洗練されたソリューションではありません。より良い方法があるはずです... –

+0

実際には、これはあなたが受け入れたアイデアと全く同じものを使用しますが、これはより少ないリソースを使用します。少なくとも2つのフォークは少なく、他の外部プログラムにはexecはありません。 – TrueY

1

あなたはこれを起動することができます。

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

これはプロジェクト用に書いてあります。私は100文字以上を使っているなら、chrとord fucntions(ここではどこかにあります)といくつかの純粋なbash(関数内で呼び出された外部のみがtrです)私のテストの短い文字列は、実際にはPythonよりも少し速いです。 また、このスクリプトはすべての入力を小文字にします。大文字に変更する必要があります。スクリプトでこれらの関数を入れた後、 (またはカットし、シェルに貼り付ける)あなただけの

abz9z9 inc_string

を行うと、取り戻すことができます。

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 

} 
関連する問題