原型: value=$((n#${key}Xm))shell
value:自定义变量获得运算的值bash
n:欲转成的进制数; 2进制就是2,10进制就是10ide
key:字符串变量spa
X:操做符;如+ - * / &...orm
m:操做数字符串
实例1:10进制字符32加上32input
a='32'原型
value=$((10#${a}+32))it
----------------------------------------------for循环
[root@localhost testshell]# echo $a
64
-------------64为10进制输出-------
实例2:16进制字符32加上32
a='32'
value=$((16#${a}+0x32))
----------------------------------------------
[root@localhost testshell]# echo $a
100
-------------100为10进制输出-------
现实使用实例:
shell经过for循环读取文件后要对文件名进行转数字操做
好比将文件名-9361,前面补2个0
shell脚本以下:
#!/bin/bash
#rename files in your input path
for file in `ls $1`
do
if [ -f $file ]
then
right=${file#*.}
left=${file%.*}
if [ $2 = ${right} ]
then
leftn=$((10#$left-9361)) name="00${leftn}" rename ${left} ${name} ${file} echo "${file}-->${name}.${right}" else echo "${file} is not $2" fi else echo "${file} is not file!" fidone