- MCP server with stdio transport for local use - Search episodes, transcripts, hosts, and series - 4,511 episodes with metadata and transcripts - Data loader with in-memory JSON storage 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
255 lines
23 KiB
Plaintext
255 lines
23 KiB
Plaintext
Episode: 4088
|
|
Title: HPR4088: Today I Learnt more Bash tips
|
|
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr4088/hpr4088.mp3
|
|
Transcribed: 2025-10-25 19:27:35
|
|
|
|
---
|
|
|
|
This is Hacker Public Radio, episode 4,088 for Wednesday, the 3rd of April 2024.
|
|
Today's show is entitled, Today I Learned More Bash Tips.
|
|
It is part of the series, Bash Scripting.
|
|
It is hosted by some guy on the internet, and is about 24 minutes long.
|
|
It carries a clean flag.
|
|
The summary is, Scotty talks about supplying options to Bash Scripts.
|
|
You are listening to a show from the Reserve Q. We are airing it now because we had free
|
|
slots that were not filled.
|
|
This is a community project that needs listeners to contribute shows in order to survive.
|
|
Please consider recording a show for Hacker Public Radio.
|
|
Hello and welcome to another episode of Hacker Public Radio.
|
|
I'm your host, some guy on the internet, and I got to make this quick.
|
|
I convinced my daughter that I was doing nothing special and she doesn't have to come
|
|
running around here, so we're going to be talking about, Today I Learned More Bash Tips.
|
|
So there's a combination of the Bash Tips, I believe originally created by Dave Morris,
|
|
and Today I Learned, which I also created by Dave Morris, so shout out to Dave Morris.
|
|
As you started, I got two Bash scripts that I'm including down here in the show notes.
|
|
I created them relatively recently, like within the last couple of days, or they were actually
|
|
modifications of a previous script, but since I had to rewrite them, technically a creation
|
|
of the new script.
|
|
So in the very first script, you'll see the names called Showtime in that script, it should
|
|
be the top script in a list there.
|
|
I should probably have Bash to the thing here, I don't know if this will work, for the
|
|
code blocks that I'm using here, or I don't know if those are comment blocks or code blocks,
|
|
but apparently you can specify which language they're written in by including the name of
|
|
the language in the top handle, whatever it's called.
|
|
The I'm using Pandoff flavor mark down to list these scripts, so hopefully they display
|
|
properly with no issues.
|
|
So at the top of the script, as with all my scripts, you're going to see a license.
|
|
So a GPO version three, a little bit of license information, followed by the name of the
|
|
script, which is Showtime, the purpose of the script, time to make a show, right?
|
|
You got to date other information in there.
|
|
Now right down here in variables, I made the script portable, so you'll see the first variable
|
|
there, which is the bin directory, it's shorthand for bin directory, it's called bin
|
|
der. You can edit that to wherever your system calls, it's binaries from, and then the rest
|
|
of the binaries called within the script should work. I'm on in a Ubuntu based distribution,
|
|
so Ubuntu calls their binaries from a USR bin, and I believe USR is universal system resources,
|
|
or people refer to it as user as well, but there you go. We got the tools used in the script,
|
|
catdate, echo, and mk directory, a couple of other variables that I use right down in there.
|
|
You're going to see those get used a little bit later on. I make my shows in the music directory,
|
|
so that's what I have there. If you want to use this, if you want to repurpose this script,
|
|
obviously you can just point where you want your shows to be created, and use your own
|
|
nomenclature. You can see how mine is set up, how I name my directories and stuff,
|
|
but yeah, there you go. Now, the purpose of this show, the thing I actually learned,
|
|
which I should have got out the way first, I was wondering how to do it, I knew it was possible,
|
|
but I never actually learned it until very recently, which is how to give those dash
|
|
options to your scripts. So, for instance, if you were to call LS, and you wanted to feed an
|
|
option to LS, you would feed a dash L for the long option, or dash capital A for all to include,
|
|
like hidden files and things like that in the output, but exclude the dot and double dot,
|
|
which represents the return directories. Those are the options that I'm referring to.
|
|
Again, I just never bothered to learn how to do it, because most of my scripts are very basic,
|
|
and then I figured, like, look, I'm going to want to start creating scripts that did not prompt,
|
|
because I have a few scripts that will prompt me with the read function to take my input,
|
|
and then put it in there, but I'm like, look, it'd be better if I could just feed an option to the script,
|
|
that way I don't have to interact with the script directly, because the purpose of the script is,
|
|
so you don't have to continuously keep interacting with things. You just fire the script,
|
|
it does a bunch of stuff, and you go to go. So, options make that a lot better, and that's what we're
|
|
doing here. I'm showing you how I've done it. There may be a better way. If so, please do a show,
|
|
we'd love to know. But the first thing you're going to see right after start is start,
|
|
just let you know where I'm starting the business within the script here. So, we got the help function.
|
|
The help function is basically a here document, shout out to black kernel. He taught me about here
|
|
documents a while back. Another thing that I'm going to point out about black kernels was since
|
|
black kernel said in one of his shows that his father once taught him, if you have to write it
|
|
twice, then it should have been a function. So, with that said, it's almost like black kernels
|
|
father taught both of us that. So, clearly, with that logic, me and black kernel are now brothers.
|
|
If you ask him about that, he may try to deny it, so don't listen to black kernel.
|
|
Now, you got the usage inside a here document. So, the usage is very simple. It tells you what you're
|
|
going to be typing in in order to get the feedback that you want. That's just a reminder from me
|
|
because I got a ton of scripts and it's good to just be able to call a dash h to know that, hey,
|
|
look, this is how you use this crazy thing you wrote. So, now that we've passed that function,
|
|
the help which will be called later on in script, we now have a quick little comment here to let
|
|
you know that we're using get ops, which is like the key ingredient here, one of the key ingredients.
|
|
So, my guess is get ops stands for get options. We're going to run a while loop here against get
|
|
ops and we're feeding into get ops. We got a set of double quotes here and within no double quotes
|
|
are the options that we're feeding in. So, we're running, as you can see here, we got the colon s,
|
|
colon r, colon q and colon h. Those are the options that I want to feed in. Now, what they mean is
|
|
s is for show r is for reserve show or reserve q rather because it's actually q, but because I
|
|
actually use q for quit, which I need, I think I need to take that out of there because now that it
|
|
no longer needs a quit because we're just feeding an option. So, we no longer need a quit. But,
|
|
I typically use q for quit as an option. So, that's why I could not use q for q, you know,
|
|
the letter q for the word q. So, I use s for show r for reserve q, q for quit, h for help. Those are
|
|
the options that get fed into get options. Now, it's looking for those options within the variable
|
|
option that is right after it. So, we run a case statement on options, the variable option.
|
|
And now, you can see down there in the very first option, which is s for show. I got a variable
|
|
call show, which takes the next bit of secret sauce, which is the variable opt arc, which I think
|
|
is optional argument. So, this is the thing that you put afterward. So, if I, if I call the script,
|
|
I would do script name space dash s for show. So, we're doing regular show. And then I would do space,
|
|
the name of this show. Now, the name of the show is going to be collected by optional argument.
|
|
All right. So, the option gets called by get gets captured by get ops. And the name of the show is
|
|
it's going to get captured by optional argument. Both of those get fed into this case statement.
|
|
And it matches up whatever option you selected in the case statement. So, the case will, you know,
|
|
define what happens afterward. You'll see for show, we'll just make a couple directories.
|
|
But I think we also, if I'm not mistaken, we go ahead and scroll a little bit further down
|
|
here. I thought I had an f statement in here somewhere. I'm reading through the markdown file
|
|
right now. So, I don't have any sort of highlighting, you know, my syntax highlighting. So, I'm
|
|
getting lost in here. I'm sure I had an f statement in here. Oh, you know what? This is the first
|
|
function. Okay. So, we have the mkq function for make, make q. Right. So, this is a show for the
|
|
standard q. So, that's what mkq stands for. And I got a bunch of stuff that I'm going to reuse
|
|
later on. And therefore, you know, making the actual directory, the making the sub directory
|
|
within the parent directory. So, that show that I want, it'll be created within the music directory.
|
|
And then I like to have a edit sub directory and a prod sub directory within the show name
|
|
directory. As well as show notes also gets created. And they're like the description handles
|
|
that all of it gives me my full directory structure as well as show notes. And then the show notes,
|
|
they just put some simple stuff in there. Just so you have a basic structure along with the
|
|
license agreement at the bottom of the the ccbysa at the bottom of the document. And now here goes
|
|
that f statement that I was looking for earlier. So, what the f statement does is it just checks to see
|
|
is that a real directory like are you actually and it's checking for a specific thing though. See,
|
|
I have say in the music directory, I have a sub directory called shows no well as hpr shows.
|
|
Now inside of shows, I think it's like the year. So it's 2023. And what happens is when 2024
|
|
grows around, which is tomorrow, this f statement will check to see if the year that we're entering
|
|
the new show that is to be created, if it matches up with whatever year it is currently.
|
|
So since there's only a 2023 in there right now when 2024 grows around, it's going to realize,
|
|
oh, there is no 2024 directory. Let's create the 2024 directory and then put the show in the 2024
|
|
directory. Or at least that's what I hope will happen. We'll find out when 2024 gets here, right?
|
|
If it doesn't work, there'll be an update show. And I'll thank those of you who are going to slam me in
|
|
the comments with, hey, your script's broken. Yeah, you know, kindly do a show. Anywho, that's what's
|
|
happening there with that f statement. It's going to give me the new year and then put the directory
|
|
structure that I enjoy for my show, you know, the name of the show, plus the sub directory edit,
|
|
prod down in there with my show notes by calling that same function again, um, MKQ. And I have the
|
|
exact same thing set up for the reserve queue, you know, I have a function MKR for make reserve show.
|
|
And it does the exact same thing as MKQ and, you know, yada yada. Uh, again, I have to delete
|
|
quit out of there. I'm going to leave it in there for now because if I try to take it out in the
|
|
hurry, I'll probably break the script and that'll cause a problem. So I'm just going to leave it in there
|
|
for now. Leave it in there for now. And it's benign. You can strip it out if you like. And there's,
|
|
you know, you'll see the help portion down there at the bottom where it calls the help function that
|
|
we created earlier that has that here document in it. So if you don't do a lot of bash scripting,
|
|
you want to know how to use here documents. This is some of the ways you could see how they're used,
|
|
you know, they're very simple. But if you've never done it before, it just sounds like a crazy term.
|
|
Here document, it sounds like a physical document that you're somehow attaching to the script.
|
|
When in reality, it's just how you redirect up, I guess input, this, well, this will be output,
|
|
whatever, you'll be able to see it in there. So that's the first script. The second one, again,
|
|
is just more of, um, creating a script. I guess it's just another example you could use for
|
|
creating a script and feeding options to it with, you know, little dash, whatever option you want.
|
|
And this one actually, what is this script? Oh, yeah, this is the one with screen. I've been using
|
|
screen a lot more and, um, there's a couple of sockets that I always create, which is app and, uh,
|
|
jabi. And I do a lot of the work inside of these sockets now so that way my main terminal,
|
|
if I have to do anything with my system, I typically use that on the main terminal or unless it's
|
|
an update, something that's going to drag out for a long time, then I just hop in jabi and do that.
|
|
But, you know, I mean, if I'm going to be looking at logs or anything like that, I'll do that in the
|
|
main terminal, but all of the other crap that I want to run in the background, I hop in the socket
|
|
and do it. And, uh, all this script does is allow me to quickly get my sockets that I typically like to
|
|
use, which are app and jabi, quickly created. And then, you know, I'm off doing my thing as well as,
|
|
I don't always like to hop into a socket just to kill the socket, right? Sometimes I just want to kill
|
|
the socket from, from the, uh, the primary session or from outside of the socket, I should say,
|
|
in this script just makes it a lot easier. So I don't have to go through the long-winded command.
|
|
I could just run the script name plus the option that I want and kill the socket. All right, so I just
|
|
quickly run us through it because again, the main thing here is the TIL, uh, adding options to a
|
|
script. You guys already know about the help, uh, function at the top up there. I got the help
|
|
function, which describes how the script works. And, uh, actually, I'll just kind of, uh, while we're
|
|
up here at the help section, I'll just go ahead and talk to you a little bit about that. So basically,
|
|
you call the script name, you feed it an option. So the first, uh, I'll talk to you about the B option
|
|
because that was the unique one I had to do a little funny business to get that to work. I wanted
|
|
the B option to be just call B and it would do something, but it, it, I don't exactly have the skill
|
|
to make that work or maybe it just was not designed to work that way, where B script name, space,
|
|
hyphen B, carriage return, ending your script will begin doing whatever it is you instructed it to do.
|
|
Instead, I had to, uh, supply something else after the option. So what I've chosen to do is supply,
|
|
yes or no, to the option B. And what that'll do is it will either create my commonly used
|
|
sockets, which are apps and jobby. Or if I hit no, you know, if I, if I supply into it, instead of
|
|
why it would just, you know, it would delete those sockets. So once I'm done working for the day,
|
|
and I'm about to shut down rather than killing each socket individually, because typically those
|
|
are the only two that I need. I can just, you know, run option B a second time when I call the script,
|
|
but instead of saying yes, say no, and it will kill those two sockets for me. So now let's just
|
|
scroll down and get down here to the bottom. All right, so you guys know about the wild loop. We're
|
|
down here at the wild loop within the script. And I left all my little, uh, my little debug
|
|
echoes that are in there. I could have cleaned those out, but I mean, why not just leave them in there.
|
|
Maybe you guys could learn something from that as well. Who knows? But uh, typically when something
|
|
goes wrong in the script and you want to know exactly where it goes wrong, I use those little
|
|
echo statements to kind of just let me know where the script is during run during it during it's,
|
|
you know, when it's running. And hopefully we make it all the way through, but if we don't,
|
|
I'll know exactly where it broke at by, you know, which echo statement did not or where it stopped at
|
|
with the echo statements. Okay, what's going on? My daughter's already causing trouble. Let me stop
|
|
for a second and go take care of that. Okay, I've returned. I don't quite remember where I left off at.
|
|
Um, let's look at the uh, show notes here and I'll just pick up where from whatever line
|
|
I'm left on. Ooh, kids. Got to love them, huh? Okay, so uh, down here we'll see, um, we'll see
|
|
an F statement where I am. What am I doing here? Oh, yeah, there we are. So what I'm doing is I'm,
|
|
I'm searching the argument that's being supplied and seeing if rather not a user type yes or no,
|
|
or not yes or no, but why or in. So if the user type, why, which stands for yes, then it would
|
|
you see the, the options being right down below it, which is a calling screen. And uh, the,
|
|
the options that screen has available to it, which I'm using the, um, hyphen, lowercase, dm,
|
|
capital S. Those are the three options that I'm calling into screen in order to create a socket
|
|
without, uh, being switched into the socket. Uh, you use the dm s capital S or with screen will
|
|
allow you to create a socket, but it will move you into that socket when you create it. Obviously,
|
|
I don't want to do that because I'm creating multiple sockets. So I don't want to be swapped into a
|
|
socket. Then have to leave the socket to then create another socket or end up in a problematic
|
|
situation where I'm inside of a socket, trying to create another socket. So you just create all
|
|
of the sockets that you need and then transfer into them when you need to. Now after creating the
|
|
sockets, we run an LS through screen, which allows screen to display the available, uh, so LS is
|
|
just list socket, uh, in, uh, bashes where you can see the L, LF. So the additional thing to check for
|
|
and what we're doing is checking if the user supplied in, which is no, then kill those sockets and
|
|
with screen, the way you kill a socket from outside of the socket, you would call the X for execute.
|
|
Then you would specify the socket with, you know, the capital S. So it's a capital X for execute,
|
|
capital S for, uh, specifying a socket, the name of the socket. And here's one of the one of the
|
|
things about when you're, uh, targeting a socket and screen, you don't have to use the full name.
|
|
So you can see with my socket names, I have it as apps for letters, APPS. Um, but when I want to kill it,
|
|
I could just do AP. I don't have to do the full APPS. Same thing with the jobby jobby. It's J-O-B-B-Y.
|
|
Uh, same thing. I just do J-O-B and then you could just kill it. You could also kill it by the P-I-D.
|
|
Um, because that's going to require a lot more writing to, uh, target the P-I-D, you can forget
|
|
about that. We're not doing that. Um, I used P-I-D. When I want to kill with a P-I-D, I do that a
|
|
little more manually, right? You know, certainly not. I'm not going to be doing all that bash just to
|
|
kill with the P-I-D. It's not necessary. And, um, we got our obligatory good heavens right down
|
|
there just in case somebody typed something other than Y or N. So if you try to put a Z in there,
|
|
you're going to get good heavens. And then the help menu will pop up, hopefully explaining that, uh,
|
|
you not do that the next time. And, uh, we got the S option down there in the case statement as well,
|
|
where, um, what does that do anyways? Okay. So that's just creating additional sockets that are,
|
|
you know, if, if for whatever reason I needed another socket, I could just run the S option
|
|
than the name of the socket would be the argument. So I'd be able to create more sockets if needed.
|
|
K is for killing sockets. So, uh, the K in that, which is being managed through case, I've already
|
|
talked about screens, uh, capital X, capital S options for screen. And I know I'm saying option for
|
|
both the script that I'm talking about as well as for the screen program. So, you know,
|
|
hopefully it makes sense if you knew just give the show another listen and, you know, if you check
|
|
the show notes, it'll be a little bit more clear. But there it is. And, um, again, those echo
|
|
statements are just sort of debug statements to let me know where it failed during the time that I was
|
|
writing it up. Uh, another thing that I want to pop out here, because we're down here at the bottom of
|
|
the show now. I'm all the way at the bottom. Uh, there's nothing to explain anything else except for.
|
|
I guess I could put a little bit more emphasis on the, uh, on the good heavens invalid input.
|
|
Obviously, if you put in anything that isn't listed within the case statement, you're going to get an
|
|
error. And you need a way to manage the error. And the best way to manage it is by, you know,
|
|
splash screen, good heavens, you know, uh, which there was a way to add a sound effect to where it
|
|
makes it sound like something thumping on your monitor on the, uh, the glass of your monitor.
|
|
Letting you know, like, hey, you did something wrong. Okay. Enough about that. And know that you
|
|
really don't need anything like that. I was just being silly. But, um, I just want to quickly talk
|
|
about indenting when you're writing in kind of script or coding or whatever. Uh, I still don't have
|
|
a clue how to indent, right? Like I, well, everything you see here is just what works for me.
|
|
My guess is there's no real, well, maybe there is because depending on where you're working, uh,
|
|
what project you're working on, they may have sort of rules about indenting. I've tried to
|
|
watch some videos on it. But, um, typically what happens is they use languages I don't understand
|
|
during the explanation. So I just get lost trying to figure out what the language is, what the code is
|
|
that they're using, what it's doing. And really don't take away anything about the actual indenting.
|
|
So, um, my understanding of it is that it's supposed to make the code a little bit more readable.
|
|
But even, even so, right? The actual practice is like the best practice of how far to indent before
|
|
you start getting into the, the good heavens range. Yeah, that, that is still not exactly a thing
|
|
that I understand. And maybe, maybe it'll be more understandable, um, with, with an
|
|
understanding of more languages. So as you learn more languages in the practice, it's from those
|
|
languages. You, you, you have a better understanding of how indenting is done. Maybe it's just done
|
|
differently in each language. So, uh, who knows, right? Or at least I don't know if you know,
|
|
there's a show. All right. Maybe you just have a technique you like to use and it may not be like
|
|
an industry standard, but you use it and it works out just fine. There you go. So if you've ever
|
|
wanted to write a script and be able to do the little dash options to supply it to your script,
|
|
now you have two good examples on how you could, uh, accomplish that. And you're welcome.
|
|
Go ahead and leave a comment down below or do a show as a, uh, comment. And I'll catch you guys
|
|
in the next episode of Hacker Public Radio. Thank you. I'm some guy on the internet signing off.
|
|
You have been listening to Hacker Public Radio at Hacker Public Radio does work. Today's show
|
|
was contributed by a HBR listener like yourself. If you ever thought of recording broadcast,
|
|
you click on our contribute link to find out how easy it really is. Hosting for HBR has been
|
|
kindly provided by an honesthost.com, the internet archive, and our syncs.net. On this
|
|
otherwise stated, today's show is released under Creative Commons, Attribution 4.0 International
|
|
License.
|