-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertNum.sh
64 lines (52 loc) · 1.57 KB
/
convertNum.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# 检查参数个数
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <number>"
exit 1
fi
# 定义单位转换函数
convert_units() {
local bytes=$1
local GB=$((bytes / 1024 / 1024 / 1024))
local remainder_gb=$((bytes % (1024 * 1024 * 1024)))
local MB=$((remainder_gb / 1024 / 1024))
local remainder_mb=$((remainder_gb % (1024 * 1024)))
local KB=$((remainder_mb / 1024))
local remainder_kb=$((remainder_mb % 1024))
local unit_str=""
if [ $GB -gt 0 ]; then
unit_str="${unit_str}${GB}GB"
fi
if [ $MB -gt 0 ]; then
unit_str="${unit_str}${MB}MB"
fi
if [ $KB -gt 0 ]; then
unit_str="${unit_str}${KB}KB"
fi
if [ $remainder_kb -gt 0 ]; then
unit_str="${unit_str}${remainder_kb}B"
fi
echo "Equivalent size: $unit_str"
}
# 获取输入参数
input=$1
# 使用正则表达式检查参数是十进制还是十六进制
if [[ $input =~ ^0[xX][0-9a-fA-F]+$ ]]; then
# 十六进制
dec_num=$((input)) # 使用内建算术运算将十六进制转换为十进制
echo "Input is hexadecimal: $input"
echo "Converted to decimal: $dec_num"
convert_units $dec_num
elif [[ $input =~ ^[0-9]+$ ]]; then
# 十进制
hex_num=$(printf '%x\n' $input)
echo "Input is decimal: $input"
echo "Converted to hexadecimal: $hex_num"
convert_units $input
else
# 输入无效
echo "Invalid input. Please enter a decimal or hexadecimal number."
echo "对于十六进制数:./convert_number.sh 0x1FF
对于十进制数:./convert_number.sh 1024"
exit 1
fi