请问如何在 linux Bash脚本 里调用 python脚本?反之呢?

sh/bash/dash/ksh/zsh等Shell脚本
回复
悬崖勒马2011
帖子: 91
注册时间: 2013-01-23 14:27
系统: ubuntu 12.04

请问如何在 linux Bash脚本 里调用 python脚本?反之呢?

#1

帖子 悬崖勒马2011 » 2015-01-19 16:54

假如执行 1.py 输出一个数:x, 执行 2.py 输出一个数:y.

1.sh里想把x和y加起来,并打印。1.sh该怎么写?

反之呢?
头像
astolia
论坛版主
帖子: 6703
注册时间: 2008-09-18 13:11

Re: 请问如何在 linux Bash脚本 里调用 python脚本?反之呢?

#2

帖子 astolia » 2015-01-19 17:05

就当成普通的外部程序调用就行了

代码: 全选

echo $(($(python 1.py) + $(python 2.py)))

代码: 全选

import commands
print int(commands.getoutput("bash 1.sh")) + int(commands.getoutput("bash 2.sh"))
悬崖勒马2011
帖子: 91
注册时间: 2013-01-23 14:27
系统: ubuntu 12.04

Re: 请问如何在 linux Bash脚本 里调用 python脚本?反之呢?

#3

帖子 悬崖勒马2011 » 2015-01-19 17:46

astolia 写了:就当成普通的外部程序调用就行了

代码: 全选

echo $(($(python 1.py) + $(python 2.py)))

代码: 全选

import commands
print int(commands.getoutput("bash 1.sh")) + int(commands.getoutput("bash 2.sh"))
非常感谢!
悬崖勒马2011
帖子: 91
注册时间: 2013-01-23 14:27
系统: ubuntu 12.04

Re: 请问如何在 linux Bash脚本 里调用 python脚本?反之呢?

#4

帖子 悬崖勒马2011 » 2015-01-19 17:55

astolia 写了:就当成普通的外部程序调用就行了

代码: 全选

echo $(($(python 1.py) + $(python 2.py)))

代码: 全选

import commands
print int(commands.getoutput("bash 1.sh")) + int(commands.getoutput("bash 2.sh"))
那么请问想把bash里的一个数作为1.py的输入(就是1.py需要一个输入量才能执行,而这个量bash里有),怎么写?反之呢?
头像
astolia
论坛版主
帖子: 6703
注册时间: 2008-09-18 13:11

Re: 请问如何在 linux Bash脚本 里调用 python脚本?反之呢?

#5

帖子 astolia » 2015-01-20 0:30

只知道伸手不知道自己思考?把参数放到要执行的命令行里就行了,数据复杂的话还可以通过临时文件来传递。不会的话找本shell脚本的书从基础学学,有编程经验的话也就半天的事
infidel
帖子: 136
注册时间: 2012-03-28 10:16

Re: 请问如何在 linux Bash脚本 里调用 python脚本?反之呢?

#6

帖子 infidel » 2015-03-06 16:36

astolia 写了:只知道伸手不知道自己思考?把参数放到要执行的命令行里就行了,数据复杂的话还可以通过临时文件来传递。不会的话找本shell脚本的书从基础学学,有编程经验的话也就半天的事
15分钟。
伸手党有时候感觉很挫气。
回复