Your ord
function is really weird. Maybe it would be better to write it as:
function ord {
printf -v ordr "%d" "'$1"
}
Then you would use it as:
TEXT=$(cat "$1")
for (( i=0; i<${#TEXT}; i++ )); do
ord "${TEXT:$i:1}"
printf '%s
' "$ordr"
done
This still leaves two problems: you won't be able to have null bytes and you won't see trailing newlines. For example (I called your script banana
and chmod +x banana
):
$ ./banana <(printf 'ab
')
97
98
Two problems show here: the null byte is removed from Bash in the TEXT=$(cat "$1")
part, as a Bash variable can't contain null bytes. Moreover, this step also trims trailing newlines.
A more robust approach would be to use read
:
while IFS= read -r -n 1 -d '' char; do
ord "$char"
printf '%s
' "$ordr"
done < "$1"
With this modification:
$ ./banana <(printf 'ab
')
97
0
98
10
Note that this script will depend on your locale. With my locale (LANG="en_US.UTF-8
):
$ ./banana <(printf 'a?
')
97
0
8450
10
whereas:
$ LANG= ./banana <(printf 'a?
')
97
0
226
132
130
10
That's to show you that Bash doesn't read bytes, but characters. So depending on how you want Bash to treat your data, set LANG
accordingly.
If your script only does that, it's much simpler to not use an ord
function at all:
#!/bin/bash
while IFS= read -r -n 1 -d '' char; do
printf '%d
' "'$char"
done < "$1"
It's that simple!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…