ipython shell Python shell怎么打开_shell脚本

python中怎么运行shell脚本?

system()

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

ipython shell Python shell怎么打开_python怎么运行shell_02

popen()

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

ipython shell Python shell怎么打开_shell脚本_03

commands

可以很方便的取得命令的输出(包括标准和错误输出)和执行状态位。

commands.getoutput('ls')这个方法只返回执行结果result不返回状态。

ipython shell Python shell怎么打开_ipython shell_04

在python中调用shell脚本

hello.sh

ipython shell Python shell怎么打开_ipython shell_05

下面的512是返回的状态码,如果eixt 0时则返回的是0.

ipython shell Python shell怎么打开_python_06

shell脚本使用python脚本的参数

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

ipython shell Python shell怎么打开_python脚本_07

执行结果如下:

ipython shell Python shell怎么打开_ipython shell_08

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

ipython shell Python shell怎么打开_shell脚本_09

执行python脚本

ipython shell Python shell怎么打开_ipython shell_10