Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell脚本:将时间和python转换为文件_Python_Python 3.x_Bash - Fatal编程技术网

Shell脚本:将时间和python转换为文件

Shell脚本:将时间和python转换为文件,python,python-3.x,bash,Python,Python 3.x,Bash,我需要编写一个bash脚本,它执行一个python程序,我需要在同一个文件中输出执行时间和结果。 我无法编辑python代码 由于有多个测试,我想在后台执行它们。 我试过这个 #!bin/bash $(time python3 program.py file1 > solAndTimeFile1.txt &) 但它根本不起作用,它只在solAndTimeFile1.txt中输出python程序结果,时间显示在终端中 我也试过: #!bin/bash $(time python3

我需要编写一个bash脚本,它执行一个python程序,我需要在同一个文件中输出执行时间和结果。 我无法编辑python代码

由于有多个测试,我想在后台执行它们。 我试过这个

#!bin/bash
$(time python3 program.py file1 > solAndTimeFile1.txt &)
但它根本不起作用,它只在solAndTimeFile1.txt中输出python程序结果,时间显示在终端中

我也试过:

#!bin/bash
$(time python3 program.py file1 > solAndTimeFile1.txt >> solAndTimeFile1.txt &)

相同的输出,对我来说就更没有意义了。

将您的命令放在大括号中,以便它在子shell中运行,您可以捕获它的输出。要将stdout和stderr重定向到一个文件,请使用
&>file
。有关更多信息,请参见
manbash

{ time python3 program.py file1; } &>solAndTimeFile1.txt &

是解构bash命令的一个很好的资源解决web的想法,肯定会帮助我提高