如何遍历 Bash 中的所有 ASCII 字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13127950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:39:55  来源:igfitidea点击:

How to iterate through all ASCII characters in Bash?

linuxbashascii

提问by Shamdor

I know how to iterate through alphabets:

我知道如何遍历字母表:

for c in {a..z}; do ...; done

But I can't figure out how to iterate through all ASCII characters. Does anyone know how?

但我不知道如何遍历所有 ASCII 字符。有谁知道怎么做?

回答by Milan

What you can do is to iterate from 0 to 127 and then convert the decimal value to its ASCII value(or back).

您可以做的是从 0 到 127 迭代,然后将十进制值转换为其 ASCII 值(或返回)。

You can use thesefunctions to do it:

您可以使用这些功能来做到这一点:

# POSIX
# chr() - converts decimal value to its ASCII character representation
# ord() - converts ASCII character to its decimal value

chr() {
  [  -lt 256 ] || return 1
  printf \$(printf '%03o' )
}

# Another version doing the octal conversion with arithmetic
# faster as it avoids a subshell
chr () {
  [  -lt 256 ] || return 1
  printf \$((/64*100+%64/8*10+%8))
}

# Another version using a temporary variable to avoid subshell.
# This one requires bash 3.1.
chr() {
  local tmp
  [  -lt 256 ] || return 1
  printf -v tmp '%03o' ""
  printf \"$tmp"
}

ord() {
  LC_CTYPE=C printf '%d' "'"
}

# hex() - converts ASCII character to a hexadecimal value
# unhex() - converts a hexadecimal value to an ASCII character

hex() {
   LC_CTYPE=C printf '%x' "'"
}

unhex() {
   printf \x""
}

# examples:

chr $(ord A)    # -> A
ord $(chr 65)   # -> 65

回答by mata

A possibility using only echos octal escape sequences:

仅使用echos 个八进制转义序列的可能性:

for n in {0..7}{0..7}{0..7}; do echo -ne "\0$n"; done

回答by Andrew Clark

Here is what I came up with for a one-liner taking some pieces from sampson-chen's and mata's answers:

这是我从 sampson-chen 和 mata 的答案中提取一些片段的单行文章:

for n in {0..127}; do awk '{ printf("%c", 
for n in {0..127}; do echo $n; done | awk '{ printf("%c", 
echo "65" | awk '{ printf("%c", 
A
); }'
); }'
); }' <<< $n; done

Or alternatively:

或者:

# ascii for A starts at 65:
ascii=65
index=1
total=26
while [[ $total -ge $index ]]
do
    letter=$(echo "$ascii" | awk '{ printf("%c", 
awk 'function utf32(i) {printf("%c%c%c%c",i%0x100,i/0x100%0x100,i/0x10000%0x100,i/0x1000000) } BEGIN{for(i=0;i<0x110000;i++){utf32(i);utf32(0xa)}}' | iconv --from-code=utf32 --to-code=utf8 | grep -a '[[:print:]]'
); }') echo "The $index'th letter is $letter" # Increment the index counter as well as the ascii counter index=$((index+1)) ascii=$((ascii+1)) done

回答by sampson-chen

Here's how you print an integer as its corresponding ascii character with awk:

以下是将整数打印为其对应的 ascii 字符的方法awk

##代码##

which will print:

这将打印:

##代码##

And here's how you might iterate through the uppercase alphabet this way:

以下是您可以通过这种方式遍历大写字母的方法:

##代码##

回答by amaurea

Well... If you really want them all, and you want it to be something script-like, you could do this, I guess:

嗯......如果你真的想要它们,并且你想要它像脚本一样,你可以这样做,我想:

##代码##

But the list is pretty huge, and not very useful. Awk may not be the most elegant way of generating the binary integers from 0 to 0x110000 - substitute something more elegant if you find it.

但是这个列表非常庞大,而且不是很有用。awk 可能不是生成从 0 到 0x110000 的二进制整数的最优雅的方法——如果你找到它,用更优雅的方法代替它。

Edit: Oh, I see you only wanted ascii. Well, I will let this answer stay here in case somebody else actually wants all the UTF printable characters.

编辑:哦,我看到你只想要 ascii。好吧,我会让这个答案留在这里,以防其他人真的想要所有 UTF 可打印字符。