生成介于63到2048之间的十六进制数的shell脚本方法:

huangapple go评论99阅读模式
英文:

how to generate hexadecimal number in range between 63 to 2048 using shell script

问题

echo $( NUMBER=0; FLOOR=63; RANGE=2048;
while [ $NUMBER -le $FLOOR ]; do \

NUMBER=$RANDOM; \

let "NUMBER %= $RANGE"; \

done; \

echo $NUMBER;)

英文:

echo $( NUMBER=0; FLOOR=63; RANGE=2048; \
while [ $NUMBER -le $FLOOR ]; do \

NUMBER=$RANDOM; \

let "NUMBER %= $RANGE"; \

done; \

echo $NUMBER;)

答案1

得分: 1

像这样:

randomintfromrange() { echo $(( ( RANDOM % ($2 - $1 +1 ) ) + $1 )); }
printf ''%x\n'' $(randomintfromrange 63 2048)

重复100次:

for ((;i++<100;)); do printf ''%x\n'' $(randomintfromrange 63 2048); done
英文:

Like this :

randomintfromrange() { echo $(( ( RANDOM % ($2 - $1 +1 ) ) + $1 )); }
printf '%x\n' $(randomintfromrange 63 2048)

To repeat 100 times:

for ((;i++<100;)); do printf '%x\n' $(randomintfromrange 63 2048); done

答案2

得分: 1

这个Bash函数可能会有所帮助:

# 用法:inrange num min max
# 示例:inrange 123456 63 2048  # => 387

inrange() {
    printf "%X" $(( $2 + $1 % ($3 - $2 + 1) ))
}

然后

for ((i=0; i<5000; i++)); do
    printf "%d\t%s\n" $i $(inrange $i 63 2048)
done
英文:

This bash function may help:

# usage: inrange num min max
# example: inrange 123456 63 2048  # =&gt; 387

inrange() {
    printf &quot;%X&quot; $(( $2 + $1 % ($3 - $2 + 1) ))
}

and then

for ((i=0; i&lt;5000; i++)); do
    printf &quot;%d\t%s\n&quot; $i $(inrange $i 63 2048)
done

</details>



huangapple
  • 本文由 发表于 2020年1月7日 01:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定