DEV Community

Script Koder
Script Koder

Posted 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 Reason security reason, and 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 (2)

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. 😊😊