Certainly! Below is a simple Bash script that reads from standard input (stdin) character by character, converts each character to its ASCII value, and uses that value to set the background color when displaying a space. This creates a colored representation of the input based on each character's ASCII value.
Bash Script: char_to_bg_color.sh
#!/bin/bash
# Function to reset terminal colors
reset_colors() {
printf "\033[0m\n"
}
# Ensure the script resets colors on exit
trap reset_colors EXIT
# Read input character by character
while IFS= read -r -n1 char; do
# Get the ASCII value of the character
# Using printf with %d and handling possible null characters
ascii=$(printf '%d' "'$char" 2>/dev/null)
# If printf fails (e.g., empty input), skip to next iteration
if [ -z "$ascii" ]; then
continue
fi
# Ensure the color value is within 0-255
color=$(( ascii % 256 ))
# Set the background color and print a space
printf "\033[48;5;%sm " "$color"
done
# Reset colors after processing
reset_colors
How It Works
-
Reading Input:
- The
while loop uses read -r -n1 char to read the input one character at a time.
IFS= and -r ensure that leading/trailing whitespaces are preserved and backslashes are treated literally.
-
Converting to ASCII:
printf '%d' "'$char" converts the character to its ASCII decimal value.
- The
2>/dev/null suppresses any errors (e.g., when trying to convert a null character).
-
Mapping to Color:
- The ASCII value is taken modulo 256 to ensure it fits within the 256-color range supported by many terminals.
color=$(( ascii % 256 )) performs this calculation.
-
Displaying the Color:
printf "\033[48;5;%sm " sets the background color using the ANSI escape code and prints a space with that background.
- The sequence
\033[48;5;{COLOR}m changes the background color where {COLOR} is the calculated value.
-
Resetting Colors:
- After processing all input, the
reset_colors function is called to reset the terminal's colors to default using \033[0m.
Usage
-
Make the Script Executable:
Save the script to a file, for example, char_to_bg_color.sh, and make it executable:
chmod +x char_to_bg_color.sh
-
Run the Script:
You can pipe input into the script. For example:
echo "Hello, World!" | ./char_to_bg_color.sh
This command will display a sequence of colored spaces, each corresponding to the background color derived from the ASCII value of the characters in "Hello, World!".
-
Interactive Input:
Alternatively, you can run the script and type input manually:
./char_to_bg_color.sh
Type your input and press Ctrl+D (EOF) to see the colored output.
Example
Running the script with the input ABC:
echo "ABC" | ./char_to_bg_color.sh
This will display three spaces with background colors corresponding to the ASCII values of A (65), B (66), and C (67).
Notes
- Terminal Compatibility: Ensure your terminal supports 256 colors for accurate color representation.
- Color Codes: The ANSI escape codes used (
48;5;{COLOR}m) set the background color. You can modify the script to change text color or other attributes by adjusting the escape sequences.
- Error Handling: The script includes basic error handling to skip characters that cannot be converted to ASCII values.
Feel free to customize and enhance the script to suit your specific needs!