234 lines
14 KiB
Plaintext
234 lines
14 KiB
Plaintext
|
|
Episode: 562
|
||
|
|
Title: HPR0562: Introduction to bash scripting
|
||
|
|
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr0562/hpr0562.mp3
|
||
|
|
Transcribed: 2025-10-07 23:06:57
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
Hello everybody, my name is Ken Fallon and this is the first in a new bash scripting tutorial
|
||
|
|
series on Harker Public Radio.
|
||
|
|
This is a series of bash scripting for Linux, Unix and Windows systems.
|
||
|
|
First of all, let's cover what bash is.
|
||
|
|
From Wikipedia, bash is a free software, Unix, Shell, written for the GNU project.
|
||
|
|
This name is an acronym, which stands for the Born Again Shell, and the name is upon
|
||
|
|
on the name Born Shell, which was an early and important Unix shell, written by Stephen
|
||
|
|
Born, and distributed in version 7 of Unix in around 1978.
|
||
|
|
And the common Christian concept of Born Again, bash was created in 1987 by Brian Fox
|
||
|
|
and in 1990, Chet, Rami became the primary maintainer.
|
||
|
|
That's all very well and good, but it doesn't actually explain a lot what the bash shell
|
||
|
|
is.
|
||
|
|
It's actually the command line for Linux, so if you're coming from Windows, it's kind
|
||
|
|
of like the dust prompt, now you might be tempted to say how 1980's, but as any system
|
||
|
|
admin, even those coming from Windows, will know the command line is very, very powerful.
|
||
|
|
You'll also hear the command line referred to as the CLI, but what about the graphical
|
||
|
|
user interface?
|
||
|
|
Now this is fine as a means of interacting with an application, but it's very difficult
|
||
|
|
to automate stuff, and more importantly, it's very inconsistent.
|
||
|
|
Now what I mean by that is over time, over languages and over desktop environments.
|
||
|
|
So taking as an example, you want to explain to somebody over the phone how you might print
|
||
|
|
Microsoft Word, so you say go to file and print and then press OK, and they are very confused.
|
||
|
|
And after about an hour you realize that they're using Word 2007, and there is no file
|
||
|
|
menu.
|
||
|
|
It is just a menu bar.
|
||
|
|
If you think these problems are limited to Windows, you'd be wrong.
|
||
|
|
For example, since the initial iteration of Ubuntu, there has been at least three different
|
||
|
|
names and locations where you would install Add and Move software.
|
||
|
|
During the entire time, the command line way of doing things of installing software has
|
||
|
|
remained the same.
|
||
|
|
Now let's take into account different languages, different test up environments, going from
|
||
|
|
KDE to GNOME to XFCE, and it makes the issue worse.
|
||
|
|
The command line is the least common denominator, and this is why many people on forums tend
|
||
|
|
to have command line solutions, which if not exactly the same on your system will often
|
||
|
|
be close enough to get you started.
|
||
|
|
The fact is that all GUIs call lower level system calls anyway.
|
||
|
|
On the command line, these calls probably have a richer or the more complicated set of
|
||
|
|
options.
|
||
|
|
OK.
|
||
|
|
So that's bash.
|
||
|
|
Now let's move on to what scripting is.
|
||
|
|
The most common use scripting is to automate a task, that is big or boring or repetitive
|
||
|
|
or all three.
|
||
|
|
A script can be as simple as a file containing one or more commands that you type a lot,
|
||
|
|
but it also can be a complex thousand line program.
|
||
|
|
That accepts arguments, that has config files, that use procedures, functions, yad yad yad
|
||
|
|
yad.
|
||
|
|
On this series, we're going to try and take you from the complete novice to give you a
|
||
|
|
good understanding of bash scripting in particular, but also programming in general.
|
||
|
|
Now if at any time there is anything in this series that you don't understand or that
|
||
|
|
I have assumed that you know about, please send an email to kenatkenfallon.com.
|
||
|
|
So what's the difference between a bash script and a real program?
|
||
|
|
Now the traditional answer to that one is that a script is interpreted while a program
|
||
|
|
is compiled.
|
||
|
|
Now that might not make a lot of sense right now.
|
||
|
|
So let me go on to explain a little bit.
|
||
|
|
Start off as a file or many files, which is a human readable ish script.
|
||
|
|
This is called source code because it's the source of the end program that you're trying
|
||
|
|
to get to.
|
||
|
|
Now there are many programming languages just like there are many oral or verbal languages.
|
||
|
|
And as with verbal communication, languages, computer languages have different words and
|
||
|
|
different grammar.
|
||
|
|
And you can use a different computer language to ask the computer to do the same thing.
|
||
|
|
The traditional example is the hello world program.
|
||
|
|
And that's a simple program that prints hello world on the screen.
|
||
|
|
I'll put a link into the show notes with a site with examples of hello world programs
|
||
|
|
written in different computer languages.
|
||
|
|
However, regardless of how computer programming language you use, at the end of the day all
|
||
|
|
computer programs are broken down into a series of ones and zeros that the computer understands.
|
||
|
|
The difference between a compiled program and a script is the point at which the conversion
|
||
|
|
from this higher level for one of the better word English is done into the ones and zeros
|
||
|
|
that the computer understands.
|
||
|
|
In a compiled program like C, every time you make a change to the line in the source code
|
||
|
|
you need to recompile the program.
|
||
|
|
This takes the source code, which is a series of commands, and translates them into a
|
||
|
|
raw byte code that the computer processor understands.
|
||
|
|
If you're running the same program on two different processors, you need to compile it
|
||
|
|
for both processors.
|
||
|
|
Now you can do this.
|
||
|
|
You can, that's called cross-compiling if you compile it on one processor with the instruction
|
||
|
|
sets needed for another one.
|
||
|
|
That's where architectures come into their own.
|
||
|
|
So you have the i3 at 6 architecture and the AMD 64 architecture.
|
||
|
|
You also have the SPARC and various different other power PC architectures.
|
||
|
|
If you wanted to run the same program on all these, you would need to recompile it for
|
||
|
|
the different architectures.
|
||
|
|
Now in an interpreted language or scripting language, the source code is translated into
|
||
|
|
byte code each time that the program is run.
|
||
|
|
The advantage is that you don't need to recompile for the different processors.
|
||
|
|
The disadvantage is that it will be slower because every instruction has to be converted
|
||
|
|
into byte code on the fly.
|
||
|
|
So the question is, why are we doing a bash scripting tutorial?
|
||
|
|
And the reason is that bash scripts have all the functionality of procedural languages,
|
||
|
|
like C or whatever.
|
||
|
|
So it has powerful features, but its system calls are other bash programs.
|
||
|
|
And if you get to know the other programs on the command line, you can include its features
|
||
|
|
and functions into your script.
|
||
|
|
So as you get better with the command line, you also get better with scripting.
|
||
|
|
So it's a win-win situation.
|
||
|
|
Okay, this episode is getting a bit long, so I want to make a start today.
|
||
|
|
And before we start, you're going to need a text editor.
|
||
|
|
Which one you use is up to you.
|
||
|
|
Even though this is about the command line, there's absolutely no reason why you can't
|
||
|
|
use a GUI text editor.
|
||
|
|
For example, Kate or G-Edit, or if you're on Windows, no pad++.
|
||
|
|
If you prefer, there's also text editors that work in the console, like Vim or Joe or
|
||
|
|
Nano.
|
||
|
|
One thing to look for in a editor is code highlighting.
|
||
|
|
That's where the words, which are the commands, that have a special meaning are highlighted
|
||
|
|
in a different color or font or in some way distinguished.
|
||
|
|
Although you probably want to start with an easier editor, I suggest you get to grips
|
||
|
|
with basic editing in VI or VIM.
|
||
|
|
Because this tends to get installed on most Unix and Linux systems.
|
||
|
|
And there is also a version for Windows, whereas EMAX isn't.
|
||
|
|
Once you're comfortable with your text editor, you also now need a way to get to the
|
||
|
|
bash console.
|
||
|
|
And depending on your desktop environment, this may be listed as something else.
|
||
|
|
My console of choice is called console, that's K-O-N-S-O-L-E.
|
||
|
|
But just like VI, the external is probably installed on any Unix or Linux system.
|
||
|
|
That has a GUI.
|
||
|
|
So let's use that one if you can't find a console on your system.
|
||
|
|
If you can, then use it.
|
||
|
|
You can usually start external by holding down the Alt key and pressing F2.
|
||
|
|
This will usually bring up a dialog box.
|
||
|
|
And you can type in the words X-Term in lowercase letters and press Center.
|
||
|
|
If a Windows opens, you're in business.
|
||
|
|
On Windows, you can also enjoy the joys of bash.
|
||
|
|
By probably the easiest way is to install the SIGWIN program.
|
||
|
|
And there will be a link in the show notes as to how to do that.
|
||
|
|
And on Windows, I suggest using the no pad++.
|
||
|
|
And links for all of this stuff will be in the show notes.
|
||
|
|
So now you have a window open, which will be black background or white text or white background
|
||
|
|
which black text doesn't really matter.
|
||
|
|
What you're probably looking at now is what's called the command prompt or the bash prompt
|
||
|
|
or the shell or the terminal or the dustbox or the console, all depending on what you do.
|
||
|
|
There's probably a flashing cursor and that is the command prompt.
|
||
|
|
It's named the prompt because it's prompting you to enter a command by flashing the cursor.
|
||
|
|
So press Center a few times and you should see the same line repeat each time you do.
|
||
|
|
The makeup of a command prompt differs from system to system.
|
||
|
|
But a common prompt is made up of the user name.
|
||
|
|
That's the user that's logged in, for example you, the at separator, the host name, that's
|
||
|
|
the name of the computer you logged into, a colon separator, the path, now that's the
|
||
|
|
location in the file system where you are.
|
||
|
|
This might be the tilde, which is in the top left hand corner of most keyboards.
|
||
|
|
It's a bit like an s rotated 90 degrees, a merge.
|
||
|
|
This is the bash shortcut and it means your home directory.
|
||
|
|
A tilde by itself is the home directory of the logged on user, namely you, and a tilde
|
||
|
|
joe means the home directory of the user joe.
|
||
|
|
And just finishing off the prompt is the dollar sign.
|
||
|
|
This means a non-rush user is logged in.
|
||
|
|
If you see an octop which is the tic-tac-toe and the incorrectly termed pound sign, it
|
||
|
|
means the root is logged on and then of course you have a space.
|
||
|
|
Everything you type after that will be considered by bash to be a command and will be treason
|
||
|
|
as such when you press the enter key.
|
||
|
|
Now, let's get on to our first bash program and following the tradition where write and
|
||
|
|
a low world program.
|
||
|
|
I want you to type these commands into the terminal.
|
||
|
|
First of all, listen to an explanation before you go hit enter.
|
||
|
|
This is a very good place for a warning.
|
||
|
|
Bash is very powerful and there's absolutely no hand-holding.
|
||
|
|
If you type a command on the computer, it's not going to ask you, sure, it's just going
|
||
|
|
to do it and sometimes you won't be even given any visible feedback that is doing anything.
|
||
|
|
If you enter the command to delete all my files and press enter, it's just going to go
|
||
|
|
delete them.
|
||
|
|
There's going to be no confirmation, there's no recycling, there is no way to recover
|
||
|
|
them.
|
||
|
|
They're gone, gone forever.
|
||
|
|
So make sure you know what you're doing before you type anything into the console.
|
||
|
|
And you also don't be accepting blindly commands from forums on the internet.
|
||
|
|
I'm going to show you later on how to get help and to check what those commands are before
|
||
|
|
you run them.
|
||
|
|
So open up your text editor.
|
||
|
|
Actually, you don't need a text editor, we're going to be entering these commands directly
|
||
|
|
onto the command line.
|
||
|
|
And we're going to create a file which you can later open up in a text editor.
|
||
|
|
But for now, we're going to do everything from the command line, including putting everything
|
||
|
|
into a text file.
|
||
|
|
So the first command we're going to do is write the location of the bash interpreter into
|
||
|
|
the text file.
|
||
|
|
And this will tell bash itself what program is running.
|
||
|
|
Bash is very friendly and that you can run other programs within it just simply by changing
|
||
|
|
the first line.
|
||
|
|
And then you can write it in another dialect.
|
||
|
|
So thinking French instead of English, although it's also going to be English.
|
||
|
|
But anyway, I digress.
|
||
|
|
The second command will actually print the greeting on the screen.
|
||
|
|
Don't worry if you don't fully understand everything yet.
|
||
|
|
We're going to cover everything here in later episodes.
|
||
|
|
Now, the first thing you need to type, everything we're going to be typing, by the way, is
|
||
|
|
all lowercase.
|
||
|
|
Unix and bash are case sensitive.
|
||
|
|
So capital H-E-L-L-O is different from lowercase H-E-L-L-O.
|
||
|
|
And lowercase H, uppercase E, lowercase L, uppercase L are also different.
|
||
|
|
So you've got to be careful about that.
|
||
|
|
Which is why things generally tend to be done in lowercase.
|
||
|
|
Okay.
|
||
|
|
I'll not command.
|
||
|
|
So you type the command echo, space, single quote, the octetorb character, exclamation
|
||
|
|
mark, forward slash, bin, forward slash, bash, another single quote, the greater than
|
||
|
|
sign, hello, dot, and then bash, and press enter.
|
||
|
|
Then the next line type, echo, space, double quotes, echo again, space, hello, space,
|
||
|
|
world, double quotes.
|
||
|
|
And then the greater than sign, the greater than sign again, and then hello, dot, bash.
|
||
|
|
Has entered the commands into the hello dot text file.
|
||
|
|
Now we need to make the text file executable.
|
||
|
|
Under bash and under unix, a file name can be have any extension.
|
||
|
|
There are special permissions assigned to the file, which states whether it's going
|
||
|
|
to be executable or not.
|
||
|
|
It can't run a program unless that flag is turned on.
|
||
|
|
And in order to do that, we need to use a bash program called chmod, which means change,
|
||
|
|
change modular, something, I looked it up in the show notes.
|
||
|
|
So it's change mod, space, the plus sign, lowcase x, space, hello, dot, bash.
|
||
|
|
And that is enough to make the program executable.
|
||
|
|
And now what we can do is type the dot forward slash hello, dot, bash.
|
||
|
|
And you should see hello world.
|
||
|
|
Congratulations, you've written your first bash program.
|
||
|
|
And now if you want to finish, only need to do is type exist, close the terminal and
|
||
|
|
you're done.
|
||
|
|
Well, that's it for the first episode.
|
||
|
|
I hope you could follow along.
|
||
|
|
If you have any feedback, good or bad, you can send it to feedback at kenfallon.com.
|
||
|
|
And you can join me next time as we go through some basic bash commands.
|
||
|
|
A lot of these we'll use in scripts later in other episodes.
|
||
|
|
Thank you for listening to AcrofovaGradio, HPR is sponsored by Carol.net, so head on over
|
||
|
|
to C-A-R-O dot anything for all of us here.
|
||
|
|
Thank you for listening to AcrofovaGradio.
|