文章目录
- 1.C++实现
- 2.python实现
1.C++实现
下面是封装好的C++头文件,直接调用run_cmd_fun()即可。
#ifndef GET_CMD_H
#define GET_CMD_H
#endif // GET_CMD_H
#include <iostream>
#include<QString>
using namespace std;
//system("gnome-terminal --window -e 'bash -c \"pwd;ls;exec bash\"' --tab -e 'bash -c \"pwd; exec bash\"'");
QString win_cmd = "gnome-terminal --window -e 'bash -c \""; // 运行窗口的命令
QString tab_cmd = " --tab -e 'bash -c \""; //运行标签的命令
QString pause_cmd = ";exec bash\"'"; //终端等待的命令
QString source_path = "source /opt/ros/noetic/setup.bash ;"; //添加环境变量
QString Qcmd = "";
void run_cmd_fun(QString cmd1)
{ //get one cmd
Qcmd = "";
Qcmd.append(win_cmd);
Qcmd.append(source_path);
Qcmd.append(cmd1);
Qcmd.append(pause_cmd);
string cmd = Qcmd.toStdString();
cout << cmd << endl;
system(cmd.c_str());
}
void run_cmd_fun(QString cmd1, QString cmd2, int input_time=1)
{ //get two cmd
//The time set
Qcmd = "";
QString pause_t = "sleep ";
pause_t.append(QString::number(input_time));
pause_t.append(";");
//The first cmd set
Qcmd.append(win_cmd);
Qcmd.append(cmd1);
Qcmd.append(pause_cmd);
//The second cmd set
Qcmd.append(tab_cmd);
Qcmd.append(pause_t);
Qcmd.append(cmd2);
Qcmd.append(pause_cmd);
//The cm1 + cm2 --> string cmd
string cmd = Qcmd.toStdString();
system(cmd.c_str());
}
void run_cmd_fun(QString cmd1, QString cmd2, QString cmd3, int input_time=1)
{ //get three cmd
Qcmd = "";
QString pause_t = "sleep ";
pause_t.append(QString::number(input_time));
pause_t.append(";");
//The first cmd set
Qcmd.append(win_cmd);
Qcmd.append(cmd1);
Qcmd.append(pause_cmd);
//The second cmd set
Qcmd.append(tab_cmd);
Qcmd.append(pause_t);
Qcmd.append(cmd2);
Qcmd.append(pause_cmd);
//The third cmd set
Qcmd.append(tab_cmd);
Qcmd.append(pause_t);
Qcmd.append(pause_t);
Qcmd.append(cmd3);
Qcmd.append(pause_cmd);
//The cmd1 + cmd2 + cmd3 --> cmd
string cmd = Qcmd.toStdString();
system(cmd.c_str());
}
void run_cmd_fun(QString cmd1, QString cmd2, QString cmd3, QString cmd4, int input_time=1)
{ //get four cmd
Qcmd = "";
//The time set
QString pause_t = "sleep ";
pause_t.append(QString::number(input_time));
pause_t.append(";");
//The first cmd set
Qcmd.append(win_cmd);
Qcmd.append(cmd1);
Qcmd.append(pause_cmd);
//The second cmd set
Qcmd.append(tab_cmd);
Qcmd.append(pause_t);
Qcmd.append(cmd2);
Qcmd.append(pause_cmd);
//The third cmd set
Qcmd.append(tab_cmd);
Qcmd.append(pause_t);
Qcmd.append(pause_t);
Qcmd.append(cmd3);
Qcmd.append(pause_cmd);
//The fourth cmd set
Qcmd.append(tab_cmd);
Qcmd.append(pause_t);
Qcmd.append(pause_t);
Qcmd.append(pause_t);
Qcmd.append(cmd4);
Qcmd.append(pause_cmd);
// cmd1 + cmd2 + cmd3 + cmd4 = cmd
string cmd = Qcmd.toStdString();
system(cmd.c_str());
}
注意:想要生成双击直接运行的程序,需要添加:
QMAKE_LFLAGS += -no-pie
2.python实现
直接调用:run_cmd([“roscore”,“rostopic list”], 2)
openvino_path = "source /opt/intel/openvino/bin/setupvars.sh;" # 添加的一些虚拟环境
ros_path = "source ~/.bashrc;"+openvino_path
win_cmd = "gnome-terminal --window -e 'bash -c \""+ros_path
tab_cmd = " --tab -e 'bash -c \""+ros_path
end_cmd = ";exec bash\"'"
def run_win(cmd):
# 运行的第一个窗口终端
cmd = win_cmd+cmd+end_cmd
return cmd
def run_tab(cmd, t):
# 运行的标签终端
delay_t = "sleep {};".format(str(t))
cmd = tab_cmd+delay_t+cmd+end_cmd
return cmd
def run_cmd(cmds, t = 2):
cmd = run_win(cmds[0])
for i in range(1, len(cmds)):
cmd = cmd + run_tab(cmds[i], t*i)
print(cmd)
os.system(cmd)