Shell脚本写的简单字符串hash函数#!/bin/bash
Letter2Int(){
if [ "$#" -ne "0" ]; then
echo "Letter2Int Wrong Paramenters!" >&2
return 1
fi
read letter
length=`echo $letter|awk '{printf("%d", length($0))}'`shell最简单脚本
if [ "$length" -ne "1" ]; then
echo "Letter2Int: more than one letter: $letter!" >&2
return 1
fi
printf "%d" "'$letter"
return 0
}
Hash(){
if [ "$#" -ne "1" ]; then
echo "Hash Wrong Paramenters!" >&2
return 1
fi
read string
length=`echo $string|awk '{printf("%d", length($0))}'`
if [ "$length" -eq "0" ]; then
echo "Hash: At least one letter should be input!" >&2
return 1
fi
iCount=1
letterSum=0
while [ "$iCount" -le "$length" ]
do
letterCur=`expr substr $string $iCount 1`
letterNumCur=`printf "%d" "'$letterCur"`
echo "$letterCur $letterNumCur"
letterSum=`expr $letterSum + $letterNumCur`
iCount=`expr $iCount + 1`
done
echo $letterSum
letterSum=`expr $letterSum % $1`
echo $letterSum
return 0
}
read letter
echo $letter|Hash 10
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论