2022年 11月 4日

python脚本执行shell命令

python脚本执行shell命令

一、执行shell命令

方式1:system()

描述:其中最后一个0是这个命令的返回值,为0表示命令执行成功。使用system无法将执行的结果保存起来。

[root@localhost(10.90.73.1):at_os]# python
Python 3.6.8 (default, May 21 2019, 23:51:36)
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import os
>>> os.system('ls')
analysis_588_baseline_compared_572.xlsx  build_tasks   docs           log.html    report.html       resource_l7          tc_author_count.sh  testcase_8040
atcli.py                                 bvtsmoke.sh   example        logs        reports           sfos-version         tc_scan.sh          testcase_l7
atlib                                    config        keyword        output.xml  requirements.txt  stash                tc_statistic.py
at_siso                                  ct_attach.sh  keywords.json  README.md   resource          structured_keywords  test_atcli.py
0
>>> os.system('git branch')
  ALL_CI
* chengbin
0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

方式2:popen()

描述:获取命令执行的结果,但是没有命令的执行状态,这样可以将获取的结果保存起来放到list中。

>>> l=os.popen(ls)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'ls' is not defined
>>> l=os.popen('ls')
>>> print(l)
<os._wrap_close object at 0x7fd885e802e8>
>>> l=os.popen('ls').read()
>>> print(l)
analysis_588_baseline_compared_572.xlsx
atcli.py
atlib
at_siso
build_tasks
bvtsmoke.sh
config
ct_attach.sh
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

方式3:commands

描述:

  • 可以很方便的取得命令的输出(包括标准和错误输出)和执行状态位。
  • commands.getoutput('ls')这个方法只返回执行结果result不返回状态。

二、执行shell脚本

场景1:执行常规脚本

描述:下面的512是返回的状态码,如果exit 0时则返回的是0.

shell脚本:hello.sh

[root@localhost(10.90.73.1):chrisbin]# cat hello.sh
#!/bin/bash
echo "hello world"
exit 2
  • 1
  • 2
  • 3
  • 4

执行脚本:

[root@localhost(10.90.73.1):chrisbin]# python
Python 3.6.8 (default, May 21 2019, 23:51:36)
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system('./hello.sh')
sh: ./hello.sh: Permission denied
32256
>>> os.system('chmod +x hello.sh')
0
>>> os.system('./hello.sh')
hello world
512

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

场景2:shell脚本使用python脚本的参数

1、写一个hello.sh脚本,需要传入两个参数:

[root@localhost(10.90.73.1):chrisbin]# cat hello.sh
#!/bin/bash
echo "hello world ${1} ${2}"
exit 2
[root@localhost(10.90.73.1):chrisbin]# ./hello.sh cheng bin
hello world cheng bin
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2、在python脚本中调用shell脚本,并传入参数,注意参数前后要有空格

[root@localhost(10.90.73.1):chrisbin]# cat testcase.py
''''
python执行shell命令
'''

import os
import sys

if (len(sys.argv)<3):
    print("please input two arguments")
    sys.exit()
arg0=sys.argv[0] #argv[0] is .py file
arg1=sys.argv[1]
arg2=sys.argv[2]

os.system('./hello.sh '+arg1+' '+arg2)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3、执行脚本

[root@localhost(10.90.73.1):chrisbin]# python testcase.py 11 22
hello world 11 22
  • 1
  • 2