【发布时间】:2018-12-26 13:54:23
【问题描述】:
我有一个可以打开终端的函数:
def open_next_terminal():
import subprocess
def rand(path="/tmp"):
acc = string.ascii_letters
retval = []
for _ in range(7):
retval.append(random.choice(acc))
return "{}/{}.sh".format(path, ''.join(retval))
file_path = rand()
with open(file_path, "a+") as data:
data.write(
'''
#!/bin/bash
"$@"
exec $SHELL
'''
)
subprocess.call(["sudo", "bash", "{}".format(file_path)])
return file_path
我想在这个新打开的终端中执行任何操作之前运行一个命令。例如:
subprocess.call(["sudo", "bash", "{}".format(file_path)]) #<= is called
ls #<= is run
#<= some output of files and folders
root@host:~# #<= the shell is now available
子进程是否允许我在 shell 初始化期间运行“第一个命令”?
【问题讨论】:
标签: python python-2.7 command subprocess