have you ever felt stuck while writing bash script, which becomes complex over time and you find no other way to make it easier?
in this blog, I'll try to demonstrate, how I've integrated the Python module with a bash script to make it easier.
What do we need? ๐
- Basic Python understanding
- Bash scripting
- free time to play with it
How to run a simple bash command using python ๐ฏ
#!/bin/python3
# import subprocess modules
import subprocess as sp
# run subprocess
sp.run('ls',shell=True)
Output:
passing argument as a list ๐
- For security reasons we don't enable shell=True, instead we pass command arguments via a list in the process
#!/bin/python3
# import subprocess modules
import subprocess as sp
# run subprocess
p1=sp.run(['ls','-la'],capture_output=True,text=True)
# text=True: this option converts the output into a string instead of a binary
# capture_output=True: this option directs output to be stored in a variable instead of directing it to the console
# printing the output
print(p1.stdout)
# more explanation:
# p1.stdout: this prints the standard output of the process
# p1.stderr: this will print the standard error of the process
# p1.returncode: this will print the return code for a process
# zero: if the command ran successfully
# non-zero: if command failed in execution
Output:
Redirecting output to a file โก๏ธ๐
- We can also store the output of a command to file
#!/bin/python3
# import subprocess modules
import subprocess as sp
# run subprocess
p1=sp.run(['ls','-la'],capture_output=True,text=True)
# using file handling we can output content of the process p2 to a file
with open('date.txt','w') as f:
p2=sp.run(['date'],stdout=f,text=True)
Output:
What if!! we get an error!! ๐ฒ
- Usually python doesn't give an error by default if a command results in an error
- to fix this we use the check argument in the subprocess command
#!/bin/python3
# import subprocess modules
import subprocess as sp
# run subprocess
p1=sp.run(['ls','-la','-e'],capture_output=True,text=True,check=True)
# printing the output
print(p1.stderr)
Output:
What if!! ๐ค we want to discard the error
- to discard the error in bash we redirect it to the > /dev/null
- to make the same happen using Python we use
#!/bin/python3
# import subprocess modules
import subprocess as sp
# run subprocess
p1=sp.run(['ls','-la','-e'],stderr=sp.DEVNULL)
# printing the output
print(p1.stderr)
Output:
Reading ๐ a file ๐
- to read a file we use cat command
#!/bin/python3
# import subprocess modules
import subprocess as sp
# run subprocess
p1=sp.run(['cat','sample-file.txt'],capture_output=True,text=True)
# printing the output
print(p1.stdout)
Output:
grep the contents of the file
- to grep the contents of the file we can use output of the previous process as input to the other process.
#!/bin/python3
# import subprocess modules
import subprocess as sp
# run subprocess to cat
p1=sp.run(['cat','sample-file.txt'],capture_output=True,text=True)
# run subprocess to grep
p2=sp.run(['grep','-n','text'],capture_output=True,text=True,input=p1.stdout)
# printing the output
print(p2.stdout)
Output:
โ ๏ธ Advanced โ ๏ธ !! content here!
- whenever we run a command using subprocess.run it holds the console till the command execution is completed, this can be time-consuming in some cases.
- to fix this we use Popen() method from subprocess
#!/bin/python3
# import subprocess modules
import subprocess as sp
# running a subprocess
p1=sp.Popen(['ping','-n', '10','google.com'],capture_output=True,text=True)
#checking for the output
p1.poll()
stdout,stderr = p1.communicate()
# explanation
# In the above command, it will ping Google 10 times
# During that process p1.poll() command will be used to check out if the above command has finished running or not
Top comments (3)
To make things even simpler, I discovered this lately:
Oh! Thanks Buddy, i'll surely explore this module as well. ๐๐
Bash scripts are essential for automating tasks in Linux environments, but when combined with Python, they become even more powerful. Pythonโs versatility allows you to integrate complex logic, data handling, and even web requests into a simple Bash script. By embedding Python code within Bash scripts, developers can automate more sophisticated processes, making their workflows more efficient and robust.
While developing and running these scripts can be rewarding, itโs essential to take breaks to avoid burnout. During the holiday season, the Rituals Advent Calendar offers a perfect way to relax. Each day reveals a thoughtful gift, whether it's a soothing fragrance or a skincare product, helping you unwind after a long coding session. Just like combining Bash and Python creates efficiency, adding a bit of relaxation to your day helps maintain balance and productivity.