I got a task to display all bash fonts/colors/background colors in table where the text is different for each variation and is being taken from file that contains 448 words/random number(I’m working with numbers). Here is my code for displaying all variations
for i in {0..8}; do for j in {30..37}; do for n in {40..47}; do echo -ne "e[$i;$j;$n""mcolors" done echo done done echo ""
Output: enter image description here
Code for generating random numbers:
#!/bin/bash for ((i=0;i<$1;i++)) do echo $RANDOM >> randomnumbers.sh done
So the question is how can I pass numbers from randomnumbers.sh to my script so “colors” line in output changes to number being taken by order from randomnumbers.sh? Thanks!
Advertisement
Answer
One simple approach is to have an open file descriptor with your random numbers, and read a line from that file whenever such a value is required.
Here, we’re using FD 3, so that other parts of your script can still read from original stdin:
#!/usr/bin/env bash # Make file descriptor 3 point to our file of random numbers (don't use .sh for data files) exec 3< randomnumbers || exit for i in {0..8}; do for j in {30..37}; do for n in {40..47}; do read -r randomNumber <&3 # read one number from FD 3 printf 'e[%s;%s;%sm%05d' "$i" "$j" "$n" "$randomNumber" # & use in the format string done printf 'n' done done printf 'n'