DEV Community

Script Koder
Script Koder

Posted on • Edited on

Bash Script using Python

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)
Enter fullscreen mode Exit fullscreen mode

Output:

Image description


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
Enter fullscreen mode Exit fullscreen mode

Output:

Image description


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)
Enter fullscreen mode Exit fullscreen mode

Output:

Image description


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)
Enter fullscreen mode Exit fullscreen mode

Output:

Image description


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)
Enter fullscreen mode Exit fullscreen mode

Output:

Image description


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)
Enter fullscreen mode Exit fullscreen mode

Output:

Image description


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)
Enter fullscreen mode Exit fullscreen mode

Output:

Image description


โš ๏ธ 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
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
pgradot profile image
Pierre Gradot

To make things even simpler, I discovered this lately:

import shlex
import subprocess

command = "ls -l -h -lrt"
split = shlex.split(command)
subprocess.run(split)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
nerdflash28 profile image
Script Koder

Oh! Thanks Buddy, i'll surely explore this module as well. ๐Ÿ˜Š๐Ÿ˜Š

Collapse
 
addeefd profile image
ADD

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.