Shell脚本:十六进制循环 [英] Shell Script: Hexadecimal Loop

查看:2166
本文介绍了Shell脚本:十六进制循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习shell脚本,写一个简单的脚本在循环递增的十六进制值。

I am trying to learn shell script and writing a simple script to increment Hex values in the loop.

下面是我的脚本:

increment=0x0001
handle=0x0001

for((i=1;i<=20;i++))
do
   echo $handle
   handle=$(($handle + $increment))
   handle=$(printf '%x' $handle)
done

下面是我的输出:

0x0001
2
3
4
5
6
7
8
9
a
1
2
3
4
5
6
7
8
9
a

这是工作的罚款,直到第10次迭代,但它是从1再次启动后。

It is working fine till 10th iteration but after that it is starting from 1 again.

任何一个可以让我知道我错了吗?

Can any one let me know my mistake?

编辑:取出后处理= $(printf的'%X'$处理)线路输出是:

After removing handle=$(printf '%x' $handle) line output is:

0x0001
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

其实我想在HEX只输出。

Actually I want output in HEX only.

推荐答案

它随你怎么打印值尝试做的printf%#X'的printf%#X'

It has to do with how you print the value try printf '%#x' or printf '%#X'

只要改变你使用打印的内容就行了领先的 0X 为: -

Just change the line you are using to print the content with a leading 0x as:-

handle=$(printf '%#x' $handle) 

(或)具有领先的十六进制字符作为 0X

handle=$(printf '%#X' $handle) 

通过更改,您得到的输出如下: -

With the changes, you get the output as below:-

$ ./script.sh 
0x0001
0x2
0x3
0x4
0x5
0x6
0x7
0x8
0x9
0xa
0xb
0xc
0xd
0xe
0xf
0x10
0x11
0x12
0x13
0x14
0x15
0x16
0x17
0x18
0x19
0x1a
0x1b
0x1c
0x1d
0x1e
0x1f
0x20

有关更多格式化选项查看这里: - HTTP://wiki.bash-hackers。组织/命令/内置/ 的printf(和) http://ss64.com/bash/printf html的

For more formatting options check here:- http://wiki.bash-hackers.org/commands/builtin/printf (and) http://ss64.com/bash/printf.html

这篇关于Shell脚本:十六进制循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆