Python запуск bash скрипта

How to run Bash commands in Python?

Bash commands in Python can be a powerful tool for automating tasks such as setting up development environment and or deploying code to server, and also integrating different tools and languages. So, In this Python article, we will learn to run Bash commands in Python Programming language. But before that a quick introduction about bash and Bash commands.

Table Of Contents

What is Bash?

Bash stands for Bourne-Again Shell which is another example of Shell. Bash is a Unix shell, which is a command-line interface for interacting with an operating system, and it is a popular shell on many Linux and Unix systems.

Here are some examples of common Bash commands:

  • ls: List the files in a directory
  • cd: Change the current directory
  • mkdir: Create a new directory
  • cp: Copy a file or directory
  • mv: Move a file or directory
  • rm: Remove a file or directory
  • echo: Print a message to the terminal

Bash also provides a number of powerful features, such as command line editing, command history, and shell scripting, which allow you to automate tasks and customize your shell environment.

Frequently Asked:

Now Lets learn about some methods which can be used to run Bash commands in Python.

Method 1 : Using OS module

We can use some functions of OS module to run Bash commands in Python. OS module comes pre-installed with python programming language and it is the one of the most used module for interaction with system.

os.system()

This os.system() is used to execute commands which is in the string format in Shells of different operating systems by creating a subshell. This method is implemented by using Standard C function system().
– Parameter : This receives a command in string format.
– SYNTAX : os.system(command)
– Return : On Windows, this returns the value returned by the system shell. On UNIX, this returns the exit status of the process.

See the example code below:

import os # this will create a new directory. os.system('mkdir shellfile') # this will list all the subdirectories # in the directory. os.system('ls')

This will create a new directory and then execute the second Bash commands, which will list all the subdirectories in the working directory.

test1.md test2.md test3.md shellfile temp.py

os.popen()

Also we can use another method of os module which is os.popen() . This method opens a pipe of command and this pipe then sends the output to another command.

Читайте также:  Python посчитать сколько строк

Here is an example code and output

import os # this will create a directory os in the working directory. os.popen('mkdir os') # this lists all the directories in the working directory. output = os.popen('dir').read() print(output)
test1.md test2.md test3.md os temp.py test4.md

Method 2 : Using subprocess module

Another method which can be used to run Bash commands in Python is by using the subprocess module. The subprocess module comes pre-installed with Python programming language and is mostly used to execute system commands. It has some features which provide edge over the os module like, this connects to input/output/error pipes and obtains their return codes.

subprocess.call()

The subporocess.call() method is used to initiate a program. This method calls/executes the command passed as an argument in the call() function. Here an array of commands can also be passed if you need to provide options and the arguments along with the shell command.

subprocess.call(args, stdin=None,stdout=None,stderr=None,shell=False)
  • args : Command that needs to be executed.
  • stdin : Standard input stream value which will be passed as os.pipe()
  • stdout : Standard output stream value which is obtained.
  • stderr : Handles error occurred from standard error stream.
  • shell : Boolean, If True executes the program in new shell.

See the example code below:

# importing subprocess module import subprocess # using subprocess to create a new directory using the mkdir command. subprocess.call('mkdir subprocess', shell=True) # listing all the directories. subprocess.call("ls",shell=True)
test1.md test2.md test3.md test4.md subprocess temp.py

subprocess.Popen()

The Popen() function is an upgraded version of call() method of subprocess module which takes a command in string format. This function creates a pope between the calling application and the command which is executed. Popen() function returns the pointer to the stream which is used to read and write to the pipe created by the Popen() function. This function does not wait for the program to be executed.

Below is an example code and output.

# importing subprocess module import subprocess # storing output of Popen() function. output = subprocess.Popen( 'mkdir subprocess', stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) # Using communicate() function extracting # error and output of Popen function() stdout,stderr = output.communicate() print(stdout) print(stderr) # Print the directories in the working directories subprocess.call('ls',shell=True)
b'' None test1.md test2.md test3.md test4.md subprocess temp.py

In the above code and example you can see by using two functions of subprocess module you can run Bash commands in Python programming language. The Popen() function returns an object which has output and error of the Popen() function in a tuple. The communicate() method can be used to extract output and tuple. As you can see in output there are no errors hence the stderr has returned None.

Читайте также:  Button html изменить цвет кнопки

Summary

In this Python article, How to run Bash commands in Python, we learned about bash and some most used commands of bash. Also we learned about functions and methods through which you can run Bash commands in Python. os.system() and subprocess.call() are for smaller tasks of bash, but the Popen() function of subprocess module and popen() of os module can be used for advanced bash works with output and inputs to other commands because these methods create pipes for I/O. You can always use any of the methods above according to your need.

Make sure to read and write example codes, in order to have a better understanding of this problem. Thanks.

Источник

Как запустить bash скрипт из python?

Всем привет.
Подскажите пожалуйста, как правильно запустить bash скрипт, который лежит на компе через бота телеграм, который написан на питоне и получить ответ от него?
Если запускать сам скрипт через питон то работает, а через бот падает, не могу разобраться как правильно вывод результата организовать.
В обычном питон скрипте делал так:

import subprocess subprocess.call("test.sh")

proc = subprocess.Popen(‘./test.sh’, stdout=subprocess.PIPE)
output = proc.stdout.read()
print output

enabl3, Разбираться с вашим test.sh. Это он возвращает

$ cat hello.sh #!/bin/bash echo -n "Hello"
$ cat test.py #!/usr/bin/python import subprocess proc = subprocess.Popen('./hello.sh', stdout=subprocess.PIPE) output = proc.stdout.read() print output

Saboteur, все, в начале скрипта было clear что бы очищать все что было до этого, закомментил, все хорошо.
Огромное спасибо.

eegmak

eegmak,
import subprocess
def ping(host):
param = ‘-t’ if platform.system().lower()==’windows’ else ‘-i’
command = [‘ping’, param, ‘0.2’, host]
return subprocess.call(command) == 0

#!/usr/bin/python import subprocess import shlex proc = subprocess.Popen(shlex.split('command arg1 arg2 arg3'), stdout=subprocess.PIPE) output = proc.stdout.read() print output

eegmak

Firstline, Алексей Сергей, В общем случае есть консольное приложение (возможно неправильно называю его скриптом), оно должно быть запущено от sudo (знаю что писать свой пароль в открытом виде не оч, но любой способ подойдет) это приложение в консоль выводит циклически данные. это консольное приложение должно быть запущено с параметрами. выхлоп приложения нужно перехватывать в скрипте питона

eegmak, вариантов куча, навскидку:
— можно весь python скрипт запустить через sudo
— можно сделать, чтобы python скрпит передал пароль в дочерний процесс
— можно настроить sudo, чтобы «вот это конкретное» приложение не заправшивало пароль
— можно приложение запустить в качестве systemd сервиса и просто парсить логи потом
— может sudo вообще не нужен? просто добавить юзера в нужную группу и он получит доступ к файлам.

eegmak

Алексей Сергей, попробовал добавить конкретное приложение в sudoers чтобы оно не запрашивало пароль, не получилось. Подскажите как реализовать вариант с передачей пароля внутри питон скрипта дочернему процессу. не совсем понимаю про systemd, мне нужно только парсить выхлоп приложения, а так я бы запустил его вручную, а позже подключился скриптом, если это вообще реализуемо..

Читайте также:  Html page link with css

Источник

How To Run Bash Commands In Python

There are different ways to run bash commands in Python. Lets start with os.system command.

How to use os.system to run Bash Command

Once we have imported the os. We can use os.system and pass it bash command. Lets try ls -ld /home command

The command is executed. We can’t capture the output with os.system

How to use subprocess.check_output to run Bash Commands

To see the output of executed command. There is another way. We need to import Python package subprocess.

import subprocess subprocess.check_output('ls -ld /home',shell=True, universal_newlines=True): 'drwxr-xr-x 14 root root 4096 Nov 28 16:12 /home\n' 

How to use subprocess.run to run Bash Commands

To capture the output in a variable use the run method.

subprocess.run(['ls','-ld','/home'],check=True, stdout=subprocess.PIPE, universal_newlines=True): CompletedProcess(args=['ls', '-ld', '/home'], returncode=0, stdout='drwxr-xr-x 14 root root 4096 Nov 28 16:12 /home\n')

We can also capture the output in a variable

output = subprocess.run(['ls','-ld','/home'],check=True, stdout=subprocess.PIPE, universal_newlines=True)

output can be printed using following command.

output.stdout 'drwxr-xr-x 14 root root 4096 Nov 28 16:12 /home\n' 

We can also printout the errors using following command.

check=True options throw an error if underlying option throws an error. Lets try again.

subprocess.run(['ls','-ld','/home1'],check=True, stdout=subprocess.PIPE, universal_newlines=True)

CalledProcessError: Command ‘[‘ls’, ‘-ld’, ‘/home1′]’ returned non-zero exit status 2.

Lets try without check=True option

subprocess.run(['ls','-ld','/home1'], stdout=subprocess.PIPE, universal_newlines=True) CompletedProcess(args=['ls', '-ld', '/home1'], returncode=2, stdout='')

Run Bash commands using Python Popen

Popen is used for complex commands, such as pipe commands in bash. Lets try a simple pipe command first.

p = subprocess.Popen(['ls','-ld','/home'],stderr=subprocess.PIPE, universal_newlines=True,stdout=subprocess.PIPE) out,err = p.communicate() print(out,err) drwxr-xr-x 14 root root 4096 Nov 28 16:12 /home

p is a Python process. We can query it anytime. Also we can close it.

Lets try accessing p again.

We got the following error.

ValueError: Invalid file object: _io.TextIOWrapper name=46 encoding='UTF-8'> 

How to pipe commands together using Python Popen

Lets calculate number of files in the home directory.

p1 = subprocess.Popen(['ls','-lrt','/home'],stderr=subprocess.PIPE, universal_newlines=True,stdout=subprocess.PIPE) p2 = subprocess.Popen(["wc", "-l"], stdin=p1.stdout, stdout=subprocess.PIPE)

Note, how we have to query p1.stdout to find the output.

Now lets look at the output.

output = p2.communicate() print(output) (b'23\n', None)

Wrap Up!

I have discussed above 5 ways to run Bash commands in Python. There is another utility called pysh which can be used to run Bash commands.

Источник

Оцените статью