[英]How to execute multiple statements in multiple terminals? (Linux, Bash)
I'm trying to write a script that opens 3 terminal windows and runs a couple of statements in those windows.我正在尝试编写一个脚本来打开 3 个终端窗口并在这些窗口中运行几个语句。 But it's not working.但它不起作用。 I've tried using the && operator as well as " " but I can't get it to work.我试过使用 && 运算符以及“”,但我无法让它工作。 I've also tried it with the statements on the same line as well as below each other.我也尝试过使用同一行以及彼此下方的语句。 The error I'm receiving is that the cd child process failed to execute stating that there is no such Directory.我收到的错误是 cd 子进程无法执行,说明没有这样的目录。 But the directory ~/Projects/catkin_ws is correct.但是目录 ~/Projects/catkin_ws 是正确的。
#!/bin/bash
# ROS opstarten
gnome-terminal -e cd ~/Projects/catkin_ws source devel/setup.bash roscore
# gazebo opstarten
gnome-terminal -e cd ~/Projects/catkin_ws
source devel/setup.bash
roslaunch cvg_sim_gazebo Qr_Chessboard.launch
# programma opstarten
gnome-terminal -e cd ~/Projects/catkin_ws
source devel/setup.bash
/usr/bin/python /home/user/Projects/catkin_ws/src/drone7_project/src/drone_program.py
If you really want to run them on separate terminals programmatically you can use a terminal multiplexer such as GNU screen for that.如果您真的想以编程方式在单独的终端上运行它们,您可以使用终端多路复用器,例如 GNU screen。
First you have to start a session:首先你必须开始一个会话:
$ screen -S demo
Then open all the terminals you need inside it with Ctrl-a c and configure their environments as needed, and then you can send commands to any screen page (tab) from your script using the "-X stuff" option (to stuff characters into a virtual screen terminal):然后使用 Ctrl-a c 打开其中需要的所有终端并根据需要配置它们的环境,然后您可以使用“-X stuff”选项(将字符填充到脚本中)将命令发送到脚本中的任何屏幕页面(选项卡)一个虚拟屏幕终端):
$ screen -S demo -p <page_number> -X stuff 'ls -l
'
Note that you also have to send the newline character to really enter the command.请注意,您还必须发送换行符才能真正输入命令。
I try add main enviornment path to bash and I success run roscore in another terminal.我尝试将主要环境路径添加到 bash,我成功地在另一个终端中运行了 roscore。
#!/bin/bash
# ROS opstarten
PATH=/opt/ros/kinetic/bin
gnome-terminal --tab -e /opt/ros/kinetic/bin/roscore
You need to quote the statements and use a statement separator between them.您需要引用语句并在它们之间使用语句分隔符。
gnome-terminal -e 'cd ~/Projects/catkin_ws; source devel/setup.bash; /usr/bin/python /home/user/Projects/catkin_ws/src/drone7_project/src/drone_program.py'
or alternatively with newline as statement separator或者用换行符作为语句分隔符
gnome-terminal -e 'cd ~/Projects/catkin_ws
source devel/setup.bash
/usr/bin/python /home/user/Projects/catkin_ws/src/drone7_project/src/drone_program.py'
However, running these commands in a separate terminal seems rather misdirected.但是,在单独的终端中运行这些命令似乎很误导。 Why don't you run them as regular background jobs in your current terminal with output to a file?为什么不在当前终端中将它们作为常规后台作业运行并输出到文件?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.