简体   繁体   English

如何在多个终端执行多条语句? (Linux,Bash)

[英]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.

相关问题 Linux Gonme:启动多个终端并在每个终端中执行一个命令 - Linux Gonme: Start multiple terminals and execute a command in each one Linux的多个FIND语句和IF条件// bash - Linux multiple FIND statements and IF condition / /bash Docker linux:如何为一个正在运行的容器启动多个控制台/终端? - Docker linux: How to start multiple console/terminals for one running container? 在多台服务器上使用php和bash在Linux服务器上执行命令的问题 - problem with execute command on multiple servers using php and bash on linux server 多个linux终端同时向所有windows发送命令 - multiple linux terminals send commands to all windows at the same time 将Linux伪终端API用于多个调试终端 - Using the linux pseudo terminal API for multiple debug terminals 如何在 Linux 上的 Bash 中一次删除多个文件? - How to delete multiple files at once in Bash on Linux? 多个终端,多个命令,lxterminal - Multiple terminals, multiple commands, lxterminal 如何在Linux中使用模式执行多个可执行文件 - How to execute multiple executables with a pattern in linux 如何使这个 bash 脚本更短更好? 有没有办法在 bash 中组合多个 curl 语句和 if 语句? - How can this bash script be made shorter and better? Is there a way to combine multiple curl statements and if statements in bash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM