如何在 python 脚本中调用 bash 命令中的 python 变量,或者我可以这样做吗?

how to call python variables inside bash commands in a python script or can I do that?

我正在尝试列出所有用户的 cronjobs

user_file = open('/root/pys/userdomains', 'r')
for line in user_file:
    print line
    splitted_line = line.split(': ')
    print splitted_line
    user_cron = splitted_line[1].split()[0]
    print user_cron
    print "crons for", user_cron, "is", CronTab(user=user_cron)

在最后一行,我可以使用

os.system('crontab -l -u user_cron')

得到类似的结果?我知道有 CronTab 选项,但在类似情况下,我可以在 bash 命令中使用 python 变量(例如 user_cron)吗?

不完全是:您需要构建要使用的实际命令字符串:将 user_cron 的字符串值附加到命令的文字部分。

os.system('crontab -l -u ' + user_cron)

是的,您可以使用 os.system 功能,但您必须 import os

import os
os.system('crontab -l -u user_cron')