Linux & Python -- Python 调用shell脚本、传递参数

  • 在shell脚本中调用shell脚本,并传入参数(重点掌握)

  先创建1个python脚本,内容如下:

复制代码
import os
import sys

if len(sys.argv)<3:
    print('Please Input Two Arguments')
    sys.exit(1)
arg0=sys.argv[1]
arg1=sys.argv[2]

os.system('./test_shell_2_para.sh '+arg0+' '+arg1)
复制代码

  创建 shell脚本:test_shell_2_para.sh,内容如下:

echo "hello world ${1} ${2}"
echo 0  

执行python脚本,效果如下:

wangju@wangju-HP-348-G4:~$ python3 pp.py
Please Input Two Arguments
wangju@wangju-HP-348-G4:~$ python3 pp.py 曹操 刘备
hello world 曹操 刘备 
原文地址:https://www.cnblogs.com/gengyufei/p/12884461.html