1 python invokes shell scripts in two ways: Os.system () and Os.popen (),
The former return value is the exit status code of the script, and the return value of the latter is the output of the script during execution.
>>>help (Os.system)
Help on built-in function system in module POSIX:
System (...)
System (command), Exit_status
Execute the command (a string) in a subshell.
>>> Help (Os.popen)
Help on built-in function popen in module POSIX:
Popen (...)
Popen (command [, mode= ' R ' [, BufSize]]), pipe
Open a pipe to/from a command returning a file object.
2 "Assume a shell script test.sh:
[Email protected]:~$ VI test.sh
[Email protected]:~$ more test.sh
#!/bin/bash
echo ' Hello python! '
echo ' Hello world! '
Exit 1
[Email protected]:~$
2.1 os.system (Command): This method returns a 16-bit binary number after the shell script is called,
The low is the signal number that kills the called script, the high level is the exit status code of the script,
That is, after the code "Exit 1" in the script executes, the high number of the return value of the Os.system function is 1, and if the low number is 0,
The return value of the function is 0x0100, and the conversion to decimal gets 256.
To get the correct return value for Os.system, you can restore the return value using the displacement operation (shift the return value right by 8 bits):
>>> Import OS
>>> Os.system ("./test.sh")
Hello python!
Hello world!
256
>>> N=os.system ("./test.sh")
Hello python!
Hello world!
>>> N
256
>>> n>>8
1
>>>
2.2 Os.popen (Command): This method of invocation is implemented by means of a pipeline, and the function returns a file object,
The content inside is the contents of the script output (which can be easily understood as the contents of the Echo Output), using Os.popen to invoke test.sh:
>> Import OS
>>> Os.popen ("./test.sh")
<open file './test.sh ', mode ' R ' at 0x7f6cbbbee4b0>
>>> F=os.popen ("./test.sh")
>>> F
<open file './test.sh ', mode ' R ' at 0x7f6cbbbee540>
>>> F.readlines ()
[' Hello python!\n ', ' Hello world!\n ']
>>>
3 "As a shell command called" LS ", you should use the Popen method to get the content, compared to the following:
>>> Import OS
>>> os.system ("ls") #直接看到运行结果
Desktop Downloads Music Public Templates Videos
Documents examples.desktop Pictures systemexit.py test.sh
0 #返回值为0, indicating successful command execution
>>> n=os.system (' ls ')
Desktop Downloads Music Public Templates Videos
Documents examples.desktop Pictures systemexit.py test.sh
>>> N
0
>>> n>>8 #将返回值右移8位 to get the correct return value
0
>>> f=os.popen (' ls ') #返回一个file对象, this file object can be related to the operation
>>> F
<open file ' ls ', mode ' R ' at 0x7f5303d124b0>
>>> F.readlines ()
[' desktop\n ', ' documents\n ', ' downloads\n ', ' examples.desktop\n ', ' music\n ', ' pictures\n ', ' public\n ', ' systemexit.py \ n ', ' templates\n ', ' test.sh\n ', ' videos\n ']
>>>
Summary: Os.popen () can implement a "pipe", the value obtained from this command can continue to be used. Because it returns a file object, you can perform related operations on the file object.
But if you want to see the results of the operation directly, then you should use Os.system, after the use of the immediate!
Python Os.system () and Os.popen ()