[Example Script] Generator ASCII characters

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

[Example Script] Generator ASCII characters

Post by 1000 »

Code: Select all

#!/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.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Example Script] Generator ASCII characters

Post by rene »

Not any particularly good idea what the point of this is, but still can't resist noting that you can replace all of that PRINT() with e.g.

Code: Select all

PRINT() {
	printf "A%bB\n" $(printf '\\x%x\n' $(seq $NUMBER_STARTING $NUMBER_ENDING)) | tee file.with.ascii.characters
}
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: [Example Script] Generator ASCII characters

Post by 1000 »

Thank you very much. Your example is wonderful. :D

The generator itself does nothing special.
Prints ASCII characters to the file and also in output.

I choose format

Code: Select all

NUMBER_STARTING = 37 ; A ASCII_CHARACTER B
NUMBER_STARTING = 38 ; A ASCII_CHARACTER B
because you can see
- its number
- how it looks
- how does it affect the letters A and B when it is in between them.

Edit
And you can check file with "cat" command

Code: Select all

cat file.with.ascii.characters

Code: Select all

cat -vet file.with.ascii.characters
or maybe

Code: Select all

xxd  file.with.ascii.characters
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Example Script] Generator ASCII characters

Post by rene »

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.
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: [Example Script] Generator ASCII characters

Post by 1000 »

[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.
Good to know.

I found the same thing in "man bash"

Code: Select all

$ 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.
And in https://lists.gnu.org/archive/html/bug- ... 00034.html
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: [Example Script] Generator ASCII characters

Post by Termy »

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.
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”