In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you what python calls the bash shell script is, the content is concise and easy to understand, can definitely make your eyes bright, through the detailed introduction of this article, I hope you can get something.
1. Os.system ()
Help (os.system)
1.1. Demo
Os.system (command): this method returns a 16-bit binary number after calling the shell script
The low order is the signal number that kills the called script, and the high order is the exit status code of the script.
That is, after the code of exit 1 in the script is executed, the high bit number of the os.system function return value is 1, if the low bit number is 0
The return value of the function is 0x0100, which is converted to decimal to get 256.
To get the correct return value of os.system, you can use the displacement operation (move the return value 8 bits to the right) to restore the return value:
> import os > os.system (". / test.sh") hello pythonically hello worldwide 256 > > n > > 812. Os.popen ()
Help (os.system)
2.1 demo
Os.popen (command): this invocation is achieved through pipes, and the function returns a file object.
The content is the output of the script (which can be simply understood as the output of echo) and the case of calling test.sh using os.popen
> > import os > os.popen (". / test.sh") > > f=os.popen (". / test.sh") > > f > f.readlines () ['hello python!\ n', 'hello world!\ n'] 3. Commands module
(1) commands.getstatusoutput (cmd), which returns the output result and status code in the form of a string, namely (status,output).
(2) commands.getoutput (cmd), which returns the output of cmd.
(3) commands.getstatus (file), which returns the execution result string of ls-l file. Getoutput is called. This method is not recommended.
4. Subprocess
Subprocess module, allows you to create many child processes, can specify the input, output and error output pipes of the child process and the child process when creating, and can get the output result and execution status after execution.
(1) subprocess.run (): a new function in python3.5 that executes the specified command and returns an instance of the CompletedProcess class that contains the execution result after waiting for the command to be executed.
(2) subprocess.call (): executes the specified command and returns the command execution status. The function is similar to os.system (cmd).
(3) subprocess.check_call (): a new function in python2.5 that executes the specified command and returns a status code if the execution is successful, otherwise an exception is thrown.
Description: subprocess.run (args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False, universal_newlines=False)
Subprocess.call (args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)
Subprocess.check_call (args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)
Args: represents the shell instruction. If the shell instruction is given as a string, such as "ls-l", you need to make shell = Ture. Otherwise, the default array represents shell variables, such as "ls", "- l".
When using more complex shell statements, you can first use the shlex.split () method of the shlex module to help format the command, and then pass it to the run () method or Popen.
4.1 demo
Stubs for subprocessBased on http://docs.python.org/2/library/subprocess.html and Python 3 stubfrom typing import Sequence, Any, Mapping, Callable, Tuple, IO, Union, Optional, List, Text_FILE = Union [None, int, IO [any]] _ TXT = Union [bytes, Text] _ CMD = Union [_ TXT, Sequence [_ TXT]] _ ENV = Union [Mapping [bytes, _ TXT], Mapping [Text, _ TXT]] # Same args as Popen.__init__def call (args: _ CMD Bufsize: int =..., executable: _ TXT =..., stdin: _ FILE =..., stdout: _ FILE =..., stderr: _ FILE =..., preexec_fn: Callable [[], Any] =..., close_fds: bool =..., shell: bool =..., cwd: _ TXT =..., env: _ ENV =. Universal_newlines: bool =..., startupinfo: Any =..., creationflags: int =...)-> int:... def check_call (args: _ CMD, bufsize: int =..., executable: _ TXT =..., stdin: _ FILE =..., stdout: _ FILE =..., stderr: _ FILE =. Preexec_fn: Callable [[], Any] =..., close_fds: bool =..., shell: bool =..., cwd: _ TXT =..., env: _ ENV =..., universal_newlines: bool =..., startupinfo: Any =. Creationflags: int =...)-> int:... # Same args as Popen.__init__ except for stdoutdef check_output (args: _ CMD, bufsize: int =..., executable: _ TXT =..., stdin: _ FILE =..., stderr: _ FILE =..., preexec_fn: Callable [[], Any] =. Close_fds: bool =..., shell: bool =..., cwd: _ TXT =..., env: _ ENV =..., universal_newlines: bool =..., startupinfo: Any =. Creationflags: int =...)-> bytes:. PIPE =. # type: intSTDOUT =. # type: intclass CalledProcessError (Exception): returncode = 0 # morally: _ CMD cmd =. # type: Any # morally: Optional [bytes] output =. # type: Any def _ init__ (self, returncode: int, cmd: _ CMD) Output: Optional [bytes] =...)-> None:... class Popen: stdin =. # type: optionally [any] stdout =. # type: optionally [any]] stderr =. # type: optionally [any]] pid = 0 returncode = 0 def _ init__ (self, args: _ CMD, bufsize: int =..., executable: Optional [_ TXT] =. Stdin: Optional [_ FILE] =..., stdout: Optional [_ FILE] =, stderr: Optional [_ FILE] =..., preexec_fn: Optional [Callable [[], Any]] =..., close_fds: bool =..., shell: bool =..., cwd: Optional [_ TXT] =..., env: Optional [_ ENV] =. Universal_newlines: bool =..., startupinfo: Optional [Any] =..., creationflags: int =...)-> None:. Def poll (self)-> int:... Def wait (self)-> int:... # morally:-> Tuple [Optional [bytes], Optional [bytes] def communicate (self, input: Optional [_ TXT] =...)-> Tuple [Any, Any]:. Def send_signal (self, signal: int)-> None:... Def terminate (self)-> None:... Def kill (self)-> None:... Def _ enter__ (self)-> 'Popen':... Def _ _ exit__ (self, type, value Traceback)-> bool:... # Windows-only: STARTUPINFO etc.STD_INPUT_HANDLE =. # type: AnySTD_OUTPUT_HANDLE =. # type: AnySTD_ERROR_HANDLE =. # type: AnySW_HIDE =. # type: AnySTARTF_USESTDHANDLES =. # type: AnySTARTF_USESHOWWINDOW =. # type: AnyCREATE_NEW_CONSOLE =. # type: AnyCREATE_NEW_PROCESS_GROUP =. # type: Any The above is what python calls the bash shell script. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.