I am still learning how to shell script and I have been given a challenge to make it easier for me to echo "Name1" "Name2"..."Name15" and I'm not too sure where to start, I've had ideas but I don't want to look silly if I mess it up. Any help?
I haven't actually tried anything just yet it's all just been mostly thought.
#This is what I wrote to start
#!/bin/bash
echo "Name1"
echo "Name2"
echo "Name3"
echo "Name4"
echo "Name5"
echo "Name6"
echo "Name7"
echo "Name8"
echo "Name9"
echo "Name10"
echo "Name11"
echo "Name12"
echo "Name13"
echo "Name14"
echo "Name15"
My expected results are obviously just for it to output "Name1" "Name2" etc. But I'm looking for a more creative way to do it. If possible throw in a few ways to do it so I can learn. Thank you.
A few esoteric solutions, from the least to the most unreasonable :
base64 encoded string :
The weird chain is your expected result encoded in base64, an encoding generally used to represent binary data as text.
base64 -d <<< weirdChain
is passing the weird chain as input to thebase64
tool and asking it to decode it, which displays your expected resultgenerate an infinite stream of "Name", truncate it, use line numbers :
yes
outputs an infinite stream of what it's passed as argument (ory
by default, used to automatize interactive scripts asking for [y/n] confirmation). Theawk
command exits once it reaches the 16th line, and otherwise prints its input (provided byyes
) followed by the line number. The truncature could as easily be done withhead -15
, and I've tried using thenl
"number line" utility orgrep -n
to number lines, but they always added the line numbers as prefix which required an extra re-formatting step.read random binary data and hope to stumble on all the lines you want to output :
strings /dev/urandom
will extract ascii sequences from the binary random source /dev/urandom,grep
will filter those which respect the format of a line of your expected output andsort
will reorder those lines in the correct order. Sincesort
needs to have a received its whole input before it reorders it and/dev/urandom
won't stop producing data, we usetimeout 1d
to stop reading from/dev/urandom
after a whole day in hope it has sifted through enough random data to find your 15 lines (I'm not sure that's even remotely likely).use an HTTP client to retrieve this page, extract the bash script you posted and execute it.
curl
is a command line tool that can be used as an HTTP client,grep
with its-A 18
parameter will select the "This is what I wrote to start" text and the 18 lines that follow,tail
will remove the first 3 lines, andeval
will execute your script.While it will be much more efficient than the previous solution, it's an even less reasonable solution because high-rep users can edit your question to make this solution execute arbitrary code on your computer. Ideally you'd be using an HTML-aware parser rather than basic string manipulation to extract the code, but we're not talking about best practices here...
The easiest (possibly not the most creative) way to do this is to use
printf
:This relies on bash brace expansion
{1..15}
to have the 15 strings.Use a for loop