Script Sourcing, Permissions, Extensions and BASH Path

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
21 hours 25 minutes
Difficulty
Intermediate
CEU/CPE
21
Video Transcription
00:00
>> Hello Cybrarians and welcome back
00:00
to the Linux plus course here at Cybrary.
00:00
I'm your instructor Rob Gills.
00:00
In today's lesson, we're going to be talking
00:00
about script sourcing and BASH path.
00:00
Upon completion of this lesson,
00:00
you're going to be able to understand
00:00
the ways a script could be run
00:00
>> and determine how to include paths
00:00
>> to interpreters in script files.
00:00
>> Generally, a script needs to be marked executable.
00:00
For example, we can use change mode u plus x,
00:00
and that's going to give the user owner
00:00
execute permissions on that file.
00:00
However, you really only need
00:00
read permissions to run a BASH script,
00:00
because you can run a BASH script with two commands.
00:00
You can use source
00:00
>> and then the path to the script,
00:00
>> or BASH and the path to the script.
00:00
>> Also, if you have to debug a BASH script,
00:00
you can run that with BASH dash x
00:00
and it will display that script
00:00
any debug output line by line as it goes through.
00:00
Now, when we're working with Linux script,
00:00
all Linux scripts have to start with the absolute path
00:00
>> to the command line interpreter
00:00
>> that's going to be used to execute the script.
00:00
>> For BASH, this is almost always bin bash,
00:00
and the first line of the script
00:00
>> always starts with the hashtag,
00:00
>> and then the exclamation point
00:00
>> followed by the absolute path.
00:00
The hashtag exclamation point combo
00:00
is sometimes called a hash bang or shabang.
00:00
Therefore, the first line of any bash script
00:00
is always shabang bin bash.
00:00
Now, it's considered best practice
00:00
>> to give your scripts an extension.
00:00
>> For example, script.sh identifies the file
00:00
>> as a bourne or bourne-again shell BASH script.
00:00
>> If we have a Python script,
00:00
we should indicate that that is a Python file
00:00
>> by using the dot py suffix.
00:00
>> If we had a perl script,
00:00
we should indicate it with a script dot pl,
00:00
that suffix indicates that it's perl.
00:00
Let's take a look at all of this with some demo time.
00:00
Here, we are back in our demo environment.
00:00
We're going to continue on in Ubuntu today.
00:00
We're in my home directory.
00:00
>> The PWD will see where I'm the user home,
00:00
>> and in my home directory,
00:00
I have that file that we talked about previously,
00:00
which is called test script.
00:00
I can run ls dash al on home rob test script dot sh.
00:00
Looking at this file,
00:00
>> we can see that I am the user owner
00:00
>> and I am the group owner,
00:00
>> and I also have read, write,
00:00
execute permissions on this file.
00:00
So if I wanted to,
00:00
>> I can run this file, no problems.
00:00
>> Now, let's take a look at another script.
00:00
Let's do an ls dash al on home test script two.
00:00
On this file, we can see that the user owner is root,
00:00
so is the group owner,
00:00
>> and we can see that the root user
00:00
>> has read and write permissions
00:00
>> while everybody else just has read permissions only.
00:00
>> Can we read the script?
00:00
>> Sure. We can actually read the script.
00:00
>> Let's do cat on home test script two.
00:00
We can see the contents of the script.
00:00
We can see that we have the shabang at the top,
00:00
so hashmark and then the exclamation point,
00:00
>> and then bin bash.
00:00
>> Then we see that all this is really doing
00:00
>> is just echoing output here to the screen.
00:00
>> It's just going to say,
00:00
>> this is a second test script to the screen
00:00
>> when we run this.
00:00
>> Let's try and do that.
00:00
>> Let's try and run the script,
00:00
>> so we can give the absolute path to the script
00:00
>> and we can see a home test script two.
00:00
>> We get permission denied.
00:00
Well, that's because we don't have rights.
00:00
We're not route, we're logged in as me.
00:00
We can do who am I,
00:00
>> we see that we're logged in as me as Rob,
00:00
>> and we don't have rights test.
00:00
We're not the route.
00:00
>> We're not in the route group.
00:00
>> We just have read permissions to this file.
00:00
That's why we can cut it out,
00:00
>> but we can't actually run it.
00:00
>> We don't have the x here,
00:00
so we don't have execute permissions.
00:00
But we can work around this.
00:00
We can use the source command.
00:00
Let's run that with source.
00:00
We can do source,
00:00
>> and then we can do home,
00:00
>> and we can run test script,
00:00
that test script to and it works for us.
00:00
Awesome.
00:00
>> What about with BASH?
00:00
>> We can do the same thing.
00:00
>> Let's do BASH,
00:00
>> and do home test script two.
00:00
>> That works too.
00:00
>> One thing to note though,
00:00
is that this test script two
00:00
has no extension or a suffix on it.
00:00
It's just test script two.
00:00
Linux will allow this, strictly speaking,
00:00
but your fellow systems administrators
00:00
>> or developers that you share this system with
00:00
>> are not going to like you very much,
00:00
because they have no idea what this file actually is.
00:00
It's just a file.
00:00
>> It doesn't look like a script,
00:00
>> but just says script on the name,
00:00
but it can be named anything.
00:00
If you name it banana,
00:00
>> and nobody would know what it is,
00:00
>> because it doesn't have a suffix at the end of it
00:00
>> that indicates that it is a script file.
00:00
>> So we would want to change that
00:00
>> and turn it into test script two dot sh,
00:00
>> because we're working with BASH.
00:00
>> But now, let's take a look at some more of that.
00:00
Let's talk about setting
00:00
the interpreter input in the script.
00:00
Let's cut out that file again.
00:00
Let's cut home script two.
00:00
At the very top of this,
00:00
we see that shabang,
00:00
that hash mark exclamation point combo,
00:00
>> the shabang,
00:00
>> and then we see the absolute path to bin bash.
00:00
Well, how do we find this?
00:00
Well, you find this by using which.
00:00
So we can do which on bash,
00:00
>> and strictly speaking, on the system,
00:00
>> it says that this is user bin bash,
00:00
>> but I'm going to show you something here.
00:00
Let's just go ahead and do an ll on bin.
00:00
What we can see is that bin is [inaudible] to user bin.
00:00
Even though I use bin bash,
00:00
>> its really going to user bin.
00:00
>> It doesn't really matter.
00:00
>> Well, strictly speaking,
00:00
>> we can use either user bin bash or bin bash,
00:00
but we always need to prepend
00:00
the hash peg and the exclamation point,
00:00
and then the full path
00:00
>> we're going to use to everything.
00:00
>> The same thing is true
00:00
>> of any other interpreters or shells.
00:00
>> If we wanted to do Python, for example,
00:00
we would do which python
00:00
to find the Python in our system,
00:00
and we see that that is at user bin Python.
00:00
Let me copy that real quick.
00:00
If I wanted to use Python in a script,
00:00
>> I would start it this way.
00:00
>> I would have my hashtag
00:00
and exclamation point, my shabang,
00:00
and then now I do user bin Python,
00:00
and that would be the very first line in the script.
00:00
Likewise, we can take a look at
00:00
any other shells on the system.
00:00
We do a cat on etc shells.
00:00
We can see all of these shells are valid login shells.
00:00
We could add those after a shabang
00:00
>> at the top of any script and use them
00:00
>> as an interpreter used in that script.
00:00
>> With that, in this lesson,
00:00
we covered the ways a script can be run
00:00
>> even without execute permissions.
00:00
>> Then we talked about how to include
00:00
paths to interpreters in script files.
00:00
Thanks so much for being here,
00:00
>> and I look forward to seeing you in the next lesson.
Up Next