Now whilst our script is okay,
it's still not perfect, and we want to create a rock‑star script.
We can add variables to the script, and that's going to make it more readable.
The header and the footer are okay,
but we really have to count very carefully the number of characters to
make sure we've got a similar number of = signs.
We can create a simple for loop to do the heavy lifting for us.
When we take a look at what we're going to create,
we'll see the details later, but we're setting variables now,
our INFO_HOSTNAME. And those variables are being populated by
the output of their associated commands.
If you take a look at the IP command,
we're using cut to make sure that we're only getting the first IP
address where we're using the field delimiter of a space.
To make sure we've got the right amount of = signs,
we can go through and put them like one through to 25.
We can determine the number that we need by counting the characters
in whatever the longest string is going to be,
which is probably going to be the kernel release.
We can then echo out additional information,
Host and variable name, IP and variable name,
Kernel and variable name.
So our information now becomes more readable in the script,
as well as more readable in our output.
We can then copy and paste the code for our header to be our footer.
Now we'll see more details of this as we dive out through to our command line,
but let's go through now and look at improving the info.sh. So we're
using loops to generate the header and footer,
and we're using variables to collect the data.
Come on, let's go through and find that command line.
So let's make this a really truly rock‑star script.
We're going to take a look at learning, not just scripting,
but some of the commands that are going to go into our script.
So the first thing that we can do from our command line is
execute the commands that we're going to put in.
So if I use hostname, well,
we probably already know that it returns the hostname of the system,
but ‑I, we can see then we're returning IP addresses.
But we only want one of these IP addresses, not both.
So we're going to go through and use a pipe,
the vertical bar, send it to our cut command to cut out field 1.
Our field separators,
the information is being separated by a space, so we can use
‑d as our delimiter and specify a space.
And we get then just the one IP address coming back.
The next thing we're going to go and take a look at is
how we can get our kernel release, or the kernel version.
We're going to implement the uname ‑r command,
and this is probably going to be the longest string that we need to print.
Rather than trying to count up each one of these characters,
we can have the system do the counting for us.
So if we go through an echo, the text that we're going to have alongside it,
Kernel:, plus a space, and then we can look at the output of a command,
so I can include the information together.
So using the $( with the command inside, uname ‑r,
we can see then that we've got kernel and 5.4.0 etc.
Now to do the counting, we use the vertical bar,
the pipe symbol, and send the output to word count,
but we want to count the number of characters,
which is m, so that's 26 characters.
We can use this then for the number of = signs that we
need to print in our header and footer.
Now that we know how we're going to generate the commands,
we can go through and edit our script, so the info.sh file.
I can move up to the top and then just start putting in our variables.
So INFO_HOST = and then the output of the hostname command.
I can't just put it =hostname because then it will
be set to the value of hostname.
I want it to be the value of the output of the hostname command.
And again our IP address can be set in a similar way,
so we can use our hostname ‑I.
But don't forget we're going to pipe this through to our
cut command. And we're going to pull out field 1, ‑f1 and
the delimiter is the space.
Make sure that you have got a space between your quotes
and then close off your parentheses.
And a more simple one where we're putting in our kernel
release, our INFO_KERNEL will call our variables, =, don't be
tempted to put spaces around the = sign.
It doesn't work in Bash. And we want the output of our you uname ‑r command.
Now next come is our echo where we're echoing out our = signs.
We've already calculated how many characters we need, and we need 26
characters. So we're going to generate this with a for loop So for i, i
just becomes a temporary variable, i for integer.
So where's that temporal variable is in then our brackets expansion 1
through to 26; we can set a range. So we're going to iterate through each
character between 1 and 26. We use the ; so Bash knows this is the end of
the line, and then we use do to start the loop. Do could be on a separate
line, but it's rather a waste as it's only two characters. We can then tab
in a little bit.
I'm just using two spaces for my echo command. It doesn't need to be indented,
but it reads better. Minus n suppresses the carriage return. We want to echo out
the =, followed by another = followed by 26 more,
don't we really, but not carriage returns. So echo ‑n suppresses
the normal carriage return that we have in echo.
But because we're suppressing the carriage return,
we want to make sure we've got a clear and empty line, so I just add in a
new empty echo, which will then just be a new line.
We can take out our existing statements where we're printing and replace
them with the echo command. And now we can put in a little bit of
descriptive text like our Host and the variable.
Make sure we've got these in double quotes so we can expand our variable.
I can paste this down, and then we can go through and change Host to
IP. Don't forget to change your variables as well.
It's very easy to change some information, but not all the
information. And then we can go through and put our INFO and
KERNEL. I'm using uppercase for my variables.
You don't have to, but it tends to be the standard. And
then we can put our Kernel information.
I can use the echo statement here.
This can be, if you like, my closing echo statement to put a new line after
everything. But I'm just going to now paste in the for loop for my header.
So I can do a three YY, move down and do a p for paste. Just delete the
extra line, and we're good to go now.
So we've put in our variables. We've got our for loop. With that in
place, as I go through now and execute the information, we can see now
my header and footer fits a little bit better because it's the exact
same length as my kernel line. As we go through and take a look at
running my git commit, I can use a Ctrl+R to get back to the last commit.
Esc then allows me to change the message. So Ctrl+R, Esc to put it on the
command line for editing, and then just replace your message.
So there we go.
We've been able to make a little bit more of a rock‑star script using some good syntax that we are learning from Bash.