Run shell command with input redirections from python 2.4?

前端 未结 3 1893
渐次进展
渐次进展 2021-02-13 18:50

What I\'d like to achieve is the launch of the following shell command:

mysql -h hostAddress -u userName -p userPassword 
databaseName < fileName
3条回答
  •  深忆病人
    2021-02-13 19:21

    As Andrey correctly noticed, the < redirection operator is interpreted by shell. Hence another possible solution:

    import os
    os.system("mysql -h " + ip + " -u " + mysqlUser + " " + dbName)
    

    It works because os.system passes its argument to the shell.

    Note that I assumed that all used variables come from a trusted source, otherwise you need to validate them in order to prevent arbitrary code execution. Also those variables should not contain whitespace (default IFS value) or shell special characters.

提交回复
热议问题