原型: 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