[no audio]
Friends here we are going to
write a simple Python script with your 'subprocess' module.
The requirement is, find bash version of Linux OS. See, if I
take my Linux OS or terminal, you know, you have bash on
your Unix-like systems and if you run 'bash --version'
you are getting some output.
But here what I need, I need this value, only 4.2.46.
That is actually my version, my bash version.
I need that output only.
I don't want entire output.
See before going to run your command with your Python, you
have to think about your requirement.
I mean whether you want to run simply your command, or you
also need some modifications on your output.
If you want to run simply your command, then just go and use
'os.system()'. That is very simple one.
But on the output if you need some modifications, as of now
see that, we need to get this output, from this we want only
this 4.2.46.
That means we need some modifications on your output.
That's why just go and run your command using 'subprocess' module.
Right. See, now
I am going to write a Python script just to get bash version,
'bash_version.py'. Guys, I am going to 'import subprocess' module.
[no audio]
See, before going to write your Python scripts using
'subprocess', right,
you just keep in your hand this document. Already
we have that from our previous video. For time being directly
I'm copying this line, but you people don't copy and paste
the code here. You just try to write, type your code
so that you will clearly, it is easy to remember your code, right,
your syntaxes for your 'subprocess'.
Now actually 'bash --version'
command is not depending on any environment variables,
that's why I can take simply 'cmd = False',
sorry, I mean 'shell = False'. If I take 'shell = False',
you know, you have to take your command as a list.
So 'bash --version',
so how to get a command as a list means, once again I'm showing
that. Just to go to your Python terminal, take your Windows command
as it is, I mean windows or Unix-like systems,
[no audio]
then try to split this in this way,
then you are getting some output. That is nothing but your
list for 'shell = False'.
That's what I am doing here directly because as of now
I know the required thing, so that's why directly I am writing
'bash --version' as a list.
Fine. After running your command, right,
it may take some time to execute your command. Anyway,
'bash --version' is a very simple command.
It won't take much time. You don't need to wait, but it is a
better practice to use
'sp.wait' because sometimes whenever you are going to
use some SSH command it will take some time to execute your
commands. That's why just try to make a habit that using
'sp.wait' in your code. Then find out your output and
error. So, you know how to get your output and error simply by
communicating with your
[no audio]
'sp'. That's it. Now guys, see based on 'rc' code
I can say that whether your command was success or failed, right?
Let me do one thing.
'If rc', if it is equal to 0, then I can say that, strongly
I can say
'print
[no audio]
("out is :"). So what is the output?
'o'. That is the variable we have taken. 'else' means, if
'rc' code is not equal to 0 means there was a fail.
So, "command was failed
[no audio]
with an error
[no audio]
and error is : ".
[no audio]
So error is 'e' we
are taking. But you can take any variable, right?
Let me save this, and run the code.
[no audio]
Let me run this with 'python3'.
We need 'python3', right?
[no audio]
Right. You are getting your output.
[no audio]
Right. Now what I am doing is I am going to take a invalid command.
Instead of your 'bash --version' if I run '- bash version',
you're getting an invalid syntax.
I mean command is not executing properly, right?
That is actually not a valid command.
So that's why what I am doing is, just I am taking your list
bash, then instead of '--version', I am taking, you should
not take, just to show that whether our code is working properly
or not for that I am taking this one.
[no audio]
Let me show your code once, and then let me run your code,
see, "Command was failed and error is : bash: version:
No such file or directory."
So something wrong with your command.
So we know that what is wrong here.
That's why be careful whenever if you are taking your commands.
[no audio]
So same command, you can also run by using 'shell = True', by
simply taking your command as a string, but on Windows,
please prefer 'shell = True' always.
Okay. Fine.
So guys, as of now you are getting your output with your Python,
but requirement is, I don't want to get this entire output
right? I need only this one.
That's why what I'm doing is,
first of all I'm going to convert your entire output into
a list. You can convert your output, which is from your 'sp', right,
'splitlines',
[no audio]
by using that you can get your output as a list. See that
you are getting as a list.
[no audio]
Right. Now what I am doing is, I am going to print your line one
by one. Just observe the logic. So I don't want to print in this way.
What I am doing is I, am taking one by one value from
your list which has been created from your output, simply
'for each_line in o.splitlines()'.
Simply I am printing 'each_line'.
See that. See the logic, right. The same way you just practice
it guys so that it will be easy for you to understand this code.
Yes, again you are getting your entire output.
But I want to print the lines which are having only 'version.
Now, it's simple.
[no audio]
See that. What I am doing is,
'if Version', in
line, in your 'each_line' then only print that line,
otherwise don't print that line.
[no audio]
Now, see the logic what you're going to get your output.
[no audio]
Yes, you are getting these two lines.
[no audio]
Right, but what you need? You need first line as of now, right,
I mean version. So as of now, you don't know RegEx concept,
what we will do is, yeah based on 'release'
I will take. 'if version and release', both words are there in
a given line then only I want to print that. See guys, I'm
going to use now here your logical operator called 'and'.
'if version in each_line and', then 'release'. 'version' as well
as 'release' both I should have in my line, then only I want
to print that line. Guys,
you need to understand about logical operator
'and'. When you have to take 'and' means, see my requirement
is, this thing should be true,
this thing should be true,
whenever these two are true at that time only
I want to execute my 'if' condition. So in that case you need
to club your two conditions with the help of 'and' operator.
So if both are true then only your final result will be true
if you use 'and' operator. But if you take 'or' operator no,
you are going to get again two lines. Just check it so that
you can understand. Now, let me run it and see the output.
Now you're getting only one line which is having 'version'
as well as 'release', but I need this part, right? I need this part.
So what I am doing is, guys, as of now you don't know about RegEx
that's why I am doing so many steps. Later
we will see again with RegEx.
It's very simple.
As of now don't concentrate on RegEx, just see the logic what I am doing.
[no audio]
Right. So now one, two, three, fourth part you are getting as, I mean
index four is your required output as of now. Let me take
this. From this I am taking index 3 value.
[no audio]
That's it. But I need only 4.2.46. Then again split this with the
help of this parentheses.
See that. What I am doing is, again I am splitting your output
based on this parentheses symbol.
[no audio]
Where is it?
[no audio]
That's it.
And then I am taking index 0. Just see first without this
one what is the output you're getting.
If I split that output with the help of parentheses what
we are getting? You are getting a list which consists of 4.2.46.
And then second value is something, but I need this value
that's why just take index as 0.
[no audio]
Let me write that index 0. So this is the final requirement
through which you are going to get your bash version 4.2.46.
Right. So guys successfully we implemented a code called
getting bash version using Python on your Unix-like systems.
Right. So guys, this is not that much important.
But the thing is how you are using your 'subprocess' for your
requirement, that is important. Getting bash version is not
the important thing, right. How you are implementing your Unix
command to get your bash version using your Python, that is
important. So that's why I have given this example, right.
Now before going to next video, please.
please try to find a java
version using your Python. Yeah, for Java
you need to take only one '-', right. If you run your code
I need to get only 1.7.0_211.
So before going to next video, please, please try to implement
by yourselves this output.
[no audio]
I need to, if I run, if I provide this command for my 'subprocess',
I mean for my Python script I need to get version as 1.7.0_211
And just I am giving one clue, in case if you are not getting
any output. Please see your error as well, see your output and error, and
try to implement your logic to get your Java -version. So
why I am giving this example is you have one important point
with your Java - version command whenever if you are running
with your Python.
Right. So before coming to next video, please try to implement the
code to get your Java version using your Python by executing
your command with the help of 'subprocess' module, right?
Okay guys, thank you for watching this video.
[no audio]