操作对象:
写个shell脚本:命名为test.sh
具体内容如下:
#!/bin/bash
echo "Hello, ${1}!"
a=${2}
b=${3}
echo $((a+b))
exit 0
命令直接传参:
苹果🍎:
栗子🌰:
xxx@xxx:~$ ./test.sh XiaoMing 10 20
Hello, XiaoMing!
30
xxx@xxx:~$ ./test.sh XiaoMing
Hello, XiaoMing!
0
xxx@xxx:~$ ./test.sh
Hello, !
0
python脚本传参:
写个python脚本:命名为python.py
具体内容如下:
#!usr/bin/env/ python3.6
import os
import sys
if(len(sys.argv)<4):
print("Please input three arguments !")
sys.exit(1)
# argv[0] is python.py file
arg0 = sys.argv[1]
arg1 = sys.argv[2]
arg2 = sys.argv[3]
os.system('./test.sh '+arg0+' '+arg1+' '+arg2)
栗子🌰:
xxx@xxx:~$ python python.py
Please input three arguments !
xxx@xxx:~$ python python.py XiaoMing
Please input three arguments !
xxx@xxx:~$ python python.py XiaoMing 10 20
Hello, XiaoMing!
30
注意事项:
问题:sh test.sh执行OK;./test.sh执行不过。
对策:给文件加个执行权限
chmod +x test.sh