[Autogenerated] Hello and a really big warm welcome to
you to this plurals site module, advanced editing using sd.
My name is andrew Mallet and I'm going to be your instructor
helping you guide you through this module,
a module where we can be taking a look at accessing
some more of the features of sd.
We'll begin by taking a look at bash scripts by
using sd inside of a bash script.
We can interrogate other scripts and decide whether
or not they have the shebang set.
If the shebang is set, we don't need to do anything if the shebang isn't set,
we can easily add it with sd.
Moving on from that.
We're going to look at another way that we can group expressions together,
we can group as we've seen with an enhanced regular expression.
However, when those expressions become more complex,
we're going to implement it using input files to set.
And we're going to see how we can implement remote edits using sd.
So we can really streamline our administration,
being able to run command lines from one server that execute edits
remotely and non interactively on other servers.
This you've got to see.
So to begin with, we're going to be looking at adding shebangs two scripts.
And in doing so, we're going to create ourselves a bash script.
That first line we can see goes through into creating a four loop.
We're looking then for all of the scripts inside of our scripts directory,
we enter our loop and populate the first line variable
with the first line of each file.
We test that if it doesn't begin with SSH shebang,
we use sd to insert the shebang at line one.
And then we're finished.
The idea of this script is to overcome many inconsistencies
that we're probably going to have in our scripts that have
been created over many years.
We're going to find that those inconsistencies manifest themselves
into perhaps some scripts not working in users shell.
If they aren't using bash by setting the sang,
we enforce that the script is going to run inside of bash.
Now we can look at slidess all day,
but your real learning is going to be coming from your own hands on practice.
So come on,
why don't you join me as we dive out through to our command line and
looking at implementing sd inside of our shell scripts.
We're going to use a git repository that git
repository has many scripts in it for us.
And we're going to be able to then using our script identify which
scripts have SSH shebang set and those that don't,
of course,
we're going to be able to ensure that we add to those
scripts that don't have the shebang, the shebang line,
but we won't touch those that do.
We don't want duplicate shebang lines.
Come on, let's dive out to our command line.
Now,
you can see I've created myself a directory called Test and
it's in this directory that I've been working.
We can make sure that we've got our git repository
if we haven't already cloned it.
So git clone and you can see the URL to the repository,
we can see that we've already got it on our system.
But if you don't have it,
just make sure that you've got the git repository inside of that repository,
I can go through and take a look in the said an arc directory.
We've got a scripts subdirectory.
And when I take a look inside of this, what we've got our whole load of scripts.
Now, of course, these are just very simple demonstration scripts.
I don't expect anything too much in this,
but some of them will have a sang and some of them won't have a SSH bang.
One of the scripts in there though.
The shebang dot S H is a script that we can work with.
Now, sometimes it is better if you can do the typing your selves.
But if you are unable to get the script up and running,
this gives us a working example if I go through and just co
escape and dot brings up that last argument.
So that puts me in the right directory.
And then she dot S H we can see then this is a script that we can use.
It is reliant on there being a test directory inside of your home directory,
its a tilda test.
And inside of that,
the sd an AKS and scripts directory and we're
going to interrogate all of these files.
Now, this does have SSH shebang.
So we don't want to be adding a shebang to this script but others have shebangs,
others don't.
Now, of course, you could make use of this script,
just make sure that its pointing through to the
correct path to your scripts directory,
the scripts directory that's inside of our repository.
But if you want to create your own script,
sometimes the typing is going to be really useful and it
doesn't matter what editor you use, I'm going to use win.
But if you prefer to use something like Nano, then do.
So I'm just calling it Q dot S H for the.
But again, the name is not really important, we'll write this script correctly.
So we're going to begin with SSH shebang.
A shebang is saying when we execute this,
we want to execute it with the bash shell.
And this is where we can get some inconsistencies.
If the shebang is not set, it will run in the users current shell.
And now we can get into their heavy lifting by
entering a four loop with a four loop,
we're populating a variable that we are calling F.
So for each iteration,
we populate the variable F for file in and then we have a list,
we're looking at our home directory and the test subdirectory,
you just make sure you put the path to where your
repository has been downloaded.
So the repository is going to be said and arc and inside of here,
we've got scripts and we want to be making sure that
we're working with dot S H files.
So that's our looping structure.
We put a semicolon and then we start our loop using do we don't need to indent
but indentation makes the script a little bit more readable.
And we can populate a variable called first lines.
So for each iteration, we populate the first line variable with,
well, would you guess it,
the first line of the file we're currently working with?
And we can do this using a subs shell.
So the dollar and parentheses and inside of that,
we're running said minus end to suppress the normal output retrieving.
Then the first line of the current file that we're working
with and then close off our parentheses and we're done and
dusted with our subs shell.
And now we start testing that first line variable by using
an if statement and then testing an enhanced string using
the double square brackets,
reading our first line variable and checking that it's not equal to opening
our double quotes and putting our hash an exclamation mark.
Now that's the start of SSH,
but some of these scripts might be running bash deliberately.
Others might be running, let's say the bone shell or the Z shell deliberately.
So we've done an enhanced test here using the double square brackets.
And that allows me to then implement wi cards in my string test.
So I close my string.
But then after the string, I can add the wild card of asterisk.
So we're looking for anything that begins with hash exclamation mark.
We can close off our enhance ss with the double square brackets,
end the line and then we're into our execution code.
We're indenting again for readability.
We can say the file name that we're working on.
So adding SSH she two, whatever file we're currently working with.
So that's where dollar F comes into play.
Now we do the work with said minus I for an in place edit.
We got the idea of this.
Now we can go to line one and insert.
So above line one of the file, we're going to add our shebang.
So hash exclamation mark slash bin slash bash,
close off our expression and working with the dollar F file.
That's the end of our test condition.
So we can then use F I to end the if statement and then we use
done to stop or close the looping structure.
Now, with that done, we can go through and save our change.
And of course, once we've got those saves made,
we can add in the execute permission.
So mode plus X for our she dot S H and now let's go through and execute it.
And we can see then the looping structure going
through the files that we have edited.
And of course,
there were more files including the shebang dot S H which haven't been
edited because they had the shebang line already.
This is starting to really make great use of non interactive
edit and making your life a lot easier.