【问题标题】:How to write a shell script to open four terminals and execute a command in each?如何编写一个shell脚本来打开四个终端并在每个终端中执行一个命令?
【发布时间】:2017-02-24 17:18:52
【问题描述】:

所以我正在尝试创建一个 shell 脚本来打开四个终端窗口(最好是 konsoles)并在每个终端窗口中运行一个命令,然后让每个终端保持打开状态,以便我可以在需要时继续在其中执行命令。

我尝试按照此处列出的说明进行操作:

  1. How to create a shell script to launch 3 terminals and execute a set of commands in each?

  1. How can I make a script that opens terminal windows and executes commands in them?

在尝试了这些细节之后,我得到的最好的结果如下:

#!/bin/bash

# some older test, doesn't work and complains and I get this message on command line: "QApplication::qAppName: Please instantiate the QApplication object first"
# I also can't enter text after command executes
#echo "Hello World!"
#exec konsole --noclose -e cat ~/.aliases 

# opens terminal but then I can't control terminal afterwards    
xterm -hold -e "echo Hello My World"

# didn't do anything
#exit 0

# didn't do anything except make me type exit an extra time where I executed my shell script
#$SHELL

编辑: 使用罗伯托的答案,我得到了四个这样的终端,但我无法输入其他命令,请注意没有像“mycomputername>”这样的提示:

编辑 2:

我找到了一种更好的方法来做我想做的事。下面的脚本将在单独的终端中执行 cmds 数组中列出的命令。所以 echo 'hello1' 将在一个终端中运行,而 echo 'hello2' 将在另一个终端中运行。对于 cmds 数组中列出的尽可能多的命令,这将继续

!/bin/bash
# Shell script to open terminals
# and execute a separate command in each

# Commands to run (one per terminal)
cmds=('echo 'hello1'', 'echo 'hello2'')

# Loop through commands, open terminal, execute command
for i in "${cmds[@]}"
do
    xterm -e "$i && /bin/tcsh" &
done

【问题讨论】:

    标签: linux shell unix scripting xterm


    【解决方案1】:

    控制台

    多个窗口

    #!/usr/bin/env bash
    konsole --noclose -e echo Hello terminal 1! &
    konsole --noclose -e echo Hello terminal 2! &
    konsole --noclose -e echo Hello terminal 3! &
    konsole --noclose -e echo Hello terminal 4! &
    

    多个标签

    #!/usr/bin/env bash
    konsole --noclose --new-tab -e echo Hello terminal 1! &
    konsole --noclose --new-tab -e echo Hello terminal 2! &
    konsole --noclose --new-tab -e echo Hello terminal 3! &
    konsole --noclose --new-tab -e echo Hello terminal 4! &
    

    【讨论】:

    • multiple windows 在我的 kUbuntue v 20.04 中工作。顺便说一句,在我的简单 /bin/bash 终端中不是multiple tabs
    【解决方案2】:

    您可以使用“for”循环和“&”在后台运行 xterm:


    #!/bin/bash
    
    # some older test, doesn't work and complains and I get this message on command line: "QApplication::qAppName: Please instantiate the QApplication object first"
    # I also can't enter text after command executes
    #echo "Hello World!"
    #exec konsole --noclose -e cat ~/.aliases
    
    for i in 1 2 3 4
    do
    # opens terminal but then I can't control terminal afterwards
    xterm -hold -e "echo Hello My World" &
    done
    
    # didn't do anything
    #exit 0
    
    # didn't do anything except make me type exit an extra time where I executed my shell script
    #$SHELL
    

    【讨论】:

    • 这样就打开了四个终端。我希望每个终端执行不同的命令(例如 helloworld1、helloworld2、..4),这里的主要问题是我无法控制任何终端。它们每个都打开,显示“Hello World”,但没有命令行可以在这些新窗口中输入其他文本。我在问题中发布了一张图片,以便您可以看到我在说什么。
    • 那么你的命令必须是“/bin/bash”而不是“echo Hello My World”
    • 或者也许是“echo Hello My World && /bin/bash”。这将执行第一个 commando (echo),如果第一个命令正确结束,将执行 shell。
    • 好的,我在您的第二条评论中执行了命令,但是当我输入 exit 时,终端不会关闭
    • 避免在“xterm”命令上使用“-hold”:“xterm -e "echo Hello World && /bin/bash" &"
    【解决方案3】:

    在 Linux Mint mate 发行版上,这将在 3 个单独的终端窗口中运行 <commands>

    $ cat START.sh
    
    mate-terminal --execute bash -c "<command1>" 
    mate-terminal --execute bash -c "<command2>" 
    mate-terminal --execute bash -c "<command3>" 
    

    杀死 START.sh 不会终止子 &lt;commands&gt;

    【讨论】:

      【解决方案4】:

      我发现这很容易:

      #!usr/bin/env bash
      echo "Enter the value of n:"
      read n
      for ((i = 0; i < n; i++ ))
      do 
         xterm -hold -e <enter command> &
         # In my case, I used : 
         # xterm -hold -e sar -P $i 2 5 &
      done
      

      差不多就是这样!祝你有美好的一天:)

      注意:对于新手,我们将其保存为文件名'.sh'。另外,请注意,这将在 n 个不同的终端上执行 n 个不同的命令。如果您愿意,您可以在每个终端上执行相同的命令,只需从 in do .... done 部分中删除 $i ;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-15
        • 1970-01-01
        • 1970-01-01
        • 2020-05-14
        • 2014-05-04
        • 1970-01-01
        • 2012-05-09
        相关资源
        最近更新 更多