🐍Python Lessons
Color Guide
Obviously the most important part, lol.
Paste this into your script.
Then this defines a new print function that you can specify colors.
Then we can simply specify our text to be printed in whatever color we want like this.
This will print out my name in red.
Python Syntax
Python can be run with the following style #!/bin/python3.
Python syntax is governed by indentation.
Basically only indentation that matters is spaces below and above each line of code.
Notice how as long as we indent things within a function, we should keep them indented the same unless we want to specify something else.
Variables & Data Types
There are multiple ways of declaring data types.
Notice that in we can simply declare them individually, while the other we could declare multiple varliables in a single line.
User Input
SYS
This allows us to pass arguments from the command line.
Notice how many different things could be done in order to pass arguments within the command line, this could also be a good lesson to input -h and different commands to our script.
SubProcess
subprocess.run()
The method subprocess.run()
will take a list of strings as a positional argument. This is mandatory as it has the bash command and arguments for it. The first item in the list is the command name and the remaining items are the arguments to the command.
Let’s see a quick example.
The above script list all the items in the current working directory as the script lies. There are no arguments to the command in the above script. We have given only the bash command. We can provide additional arguments to the ls
command like -l
, -a
, -la
, etc.
Let’s see a quick example with command arguments.
The above command displays all the files including hidden files along with the permissions. We have provided the argument la
which displays files and directories extra information and hidden files.
We may end up making some mistakes while writing the commands. Errors will raise according to the mistakes. What if you want to capture them and use them later? Yeah, we can do that using the keyword argument stderr.
Let’s see an example.
Make sure you don’t have the file with the name sample.txt in the working directory. The value to the keyword argument stderr is PIPE which helps to return the error in an object. We can access it later with the same name. And the keyword argument text helps to tell that the output should be a string.
Similarly, we can capture the output of the command using the stdout keyword argument.
subprocess.Popen()
The class subprocess.Popen() is advanced than the method subprocess.run(). It gives us more options to execute the commands. We will create an instance of the subprocess.Popen() and use it for various things like knowing the status of the command execution, getting output, giving input, etc..,
There are several methods of the class subprocess.Popen() that we need to know. Let’s see them one by one along with the code examples.
If you see the output for the above code, then you will realize that wait
is actually working. The print statement is executed after the completion of the command execution.
Executing Bash Scripts
We have seen two ways to execute the commands. Now, let’s see how to execute the bash scripts in Python scripts.
The subprocess has a method called call. This method is used to execute the bash scripts. The method returns the exit code from the bash script. The default exit code for the bash scripts is 0. Let’s see an example.
Create a bash script with the name practice.sh as follows.
Now, write a Python script execute the above bash script.
subprocess.run() – input
You can give input to the commands using the input keyword argument. We will give inputs in a string format. So, we need to set the keyword argument text to True
. By default, it takes it in bytes.
Let’s look at an example.
In the above program, the Python script add.py will take two numbers as input. We have given the input to the Python script using the input keyword argument.
Combining Variables
So if i want the python script to run different commands or scripts i can make them assign an environmental variable.
This can then be put into a script that will export these vraibles to run other binaries that i want.
I can also create a bash script that reads the ip and echoes it as the env variable and later on unsets it.
So basically a bash script that does the above. Prompts the user for the IP address and later on sets the IP as an enviromental variable and passes it on to lets say dirsearch.
We also gotta make sure that in python we must pass the bash command "source ./script.sh" if we are gonna do this before doing so.
Last updated