#!/bin/bash
# Script designed to create file with ascii characters.
VERSION="2"
LICENCE="GPL v3: https://www.gnu.org/licenses/gpl.html "
LC_ALL=C
## Links about ASCII :
## https://www.aivosto.com/articles/control-characters.html
## https://computersciencewiki.org/images/3/3d/Ascii_table.png
##============{
CLEAR_FILE() {
[ -f file.with.ascii.characters ] && rm -v file.with.ascii.characters
}
##============}
##===================={
NOTE_ABOUT_THE_END() {
echo " "
echo "File file.with.ascii.characters with ASCII characters ready."
if [ -f /usr/share/tuxtype/sounds/win.wav ] ; then
## Play sound :)
aplay /usr/share/tuxtype/sounds/win.wav > /dev/null 2>&1
fi
}
##====================}
##============================{
PRINT() {
while [ "$NUMBER_STARTING" -le "$NUMBER_ENDING" ] ; do
printf "%s" "NUMBER_STARTING = $NUMBER_STARTING ; "
## Print number decimal to hex
DEC_NUM=$(printf "%x" "$NUMBER_STARTING")
WC=$(wc -m <<< "$DEC_NUM")
if [ "$WC" -eq 2 ] ; then
DEC_NUM="0${DEC_NUM}"
fi
## Print text + hex char + text
printf "A\x${DEC_NUM}B\n" | tee -a file.with.ascii.characters
NUMBER_STARTING=$[$NUMBER_STARTING+1]
done
}
##============================}
case "$1" in
"--control"|"-c")
CLEAR_FILE
NUMBER_STARTING=0 ; NUMBER_ENDING=31 ; PRINT ; NUMBER_STARTING=127 ; NUMBER_ENDING=127 ; PRINT
NOTE_ABOUT_THE_END
;;
"--printable"|"-p")
CLEAR_FILE
NUMBER_STARTING=32 ; NUMBER_ENDING=126 ; PRINT
NOTE_ABOUT_THE_END
;;
"--extended"|"-e")
CLEAR_FILE
NUMBER_STARTING=128 ; NUMBER_ENDING=255 ; PRINT
NOTE_ABOUT_THE_END
;;
"--all"|"-a")
CLEAR_FILE
NUMBER_STARTING=0 ; NUMBER_ENDING=255 ; PRINT
NOTE_ABOUT_THE_END
;;
"--help"|"-h")
echo "---------------------------------------------------------"
echo "usage: $0 --option"
echo " "
echo " "
echo " Main options:"
echo " "
echo " --control -c Control ASCII characters (0-31 + 127)"
echo " "
echo " --printable -p Printable ASCII characters (32-126)"
echo " Note:"
echo " 32 -space. Space has a dual nature."
echo " It can be classified as both a control ascii character"
echo " and a (non-printing) graphic character."
echo " "
echo " --extended -e The extended ASCII characters (128-255)"
echo " "
echo " --all -a All ASCII characters (0-255)"
echo " "
echo " --help -h Show help"
echo " "
echo "---------------------------------------------------------"
echo " "
echo " - The script always writes characters to the file - file.with.ascii.characters"
echo " - The old file is always deleted before new characters are displayed ! "
echo " "
echo "-------------------------------"
exit
;;
*)
echo " Error: unknown option"
echo " Try use: $0 --help"
exit
;;
esac
Edited
I removed "/your/path/" from help. It was my mistake.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason:Topic automatically closed 6 months after creation. New replies are no longer allowed.
Ah, see you added that "NUMBER_STARTING = " print prefix when/after I posted the above. Termy here on the forum noted that printf behaviour of reusing the format string to exhaust arguments like that in a thread a while ago and I've been abusing it ever since; makes a lot of things a lot shorter.
[EDIT] Oh, and the thing I wanted to add: do note that $[..] is an obsolete alternative for $((..)) and not one guaranteed to still be around for ever/long.
[EDIT] Oh, and the thing I wanted to add: do note that $[..] is an obsolete alternative for $((..)) and not one guaranteed to still be around for ever/long.
$ man bash | grep -A15 ' Arithmetic Expansion'
Arithmetic Expansion
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The
format for arithmetic expansion is:
$((expression))
[b]The old format $[expression] is deprecated and will be removed in upcoming versions of bash.[/b]
The expression is treated as if it were within double quotes, but a double quote inside the parentheses is not
treated specially. All tokens in the expression undergo parameter and variable expansion, command substitu‐
tion, and quote removal. The result is treated as the arithmetic expression to be evaluated. Arithmetic ex‐
pansions may be nested.
The evaluation is performed according to the rules listed below under ARITHMETIC EVALUATION. If expression is
invalid, bash prints a message indicating failure and no substitution occurs.
rene wrote: ⤴Thu Dec 02, 2021 9:57 am
[EDIT] Oh, and the thing I wanted to add: do note that $[..] is an obsolete alternative for $((..)) and not one guaranteed to still be around for ever/long.
Thank you for saying that, as I had no idea, to my surprise. I wasn't convinced at first, but checked the man page and online, only to confirm what that it is indeed deprecated syntax. That being said, I recently compiled BASH 5.1 (the latest) and found that particular form of arithmetic expansion is still available, so it'll probably be around for a few more years to many of us on and targetting LTS-type systems.