bash 如何使用运行各种程序的多个选项卡启动 KDE konsole?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32779281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:38:16  来源:igfitidea点击:

How to launch a KDE konsole with multiple tabs running various progs?

bashdbuskdekonsole

提问by dargaud

I know how to start a Konsole with one executable running in it, and leave the Konsole open after the program ends. I can do this using a .desktopfile and change some options in it.

我知道如何在其中运行一个可执行文件的情况下启动 Konsole,并在程序结束后让 Konsole 保持打开状态。我可以使用.desktop文件执行此操作并更改其中的一些选项。

But I would like one step further, to launch a KDE konsole with multiple tabs open, each running a particular program, and that when the program finishes it stays open and give you a prompt.

但我想更进一步,启动一个 KDE konsole,打开多个选项卡,每个选项卡运行一个特定的程序,当程序完成时,它保持打开状态并给你一个提示。

There's no man page for Konsole so I don't even know what options it can take. Or some d-bus calls? Thanks

Konsole 没有手册页,所以我什至不知道它可以采取哪些选项。或者一些 d-bus 电话?谢谢

采纳答案by CaptainCrunch

Who ever sees beauty in the accepted solution is hopefully not in software development : ) This mustbe a one liner or a bug report must be submitted. Every other common terminal has this option. I did some research and the "almost one liner solution" is this:

希望在已接受的解决方案中看到美的人不会在软件开发中 :) 这必须是单行的,或者必须提交错误报告。每个其他公共终端都有这个选项。我做了一些研究,“几乎一个班轮解决方案”是这样的:

  1. Create a file configuring your tabulators like so and name it let's say "tabs":

    title: %n;; command: /usr/bin/htop

    title: %n;; command: /usr/bin/ncmpcpp

  1. 创建一个文件来配置您的制表符,并将其命名为“选项卡”:

    标题:%n;; 命令:/usr/bin/htop

    标题:%n;; 命令:/usr/bin/ncmpcpp

(Here's the full documentation: https://docs.kde.org/stable5/en/applications/konsole/command-line-options.htmlThe called command binaries are examples. The "%n" will name the tab exactly like the command)

(这是完整的文档:https: //docs.kde.org/stable5/en/applications/konsole/command-line-options.html被调用的命令二进制文件是示例。“%n”将完全像命令)

  1. Execute it like so:

    konsole --tabs-from-file path_to_tabs_file/tabs

  1. 像这样执行它:

    konsole --tabs-from-file path_to_tabs_file/tabs

Result: A new konsole window with 3 tabs, running defined binaries and one empty prompt. I couldn't get a bash script to run. But I did just a few minutes of testing.

结果:一个新的 konsole 窗口有 3 个选项卡,运行定义的二进制文件和一个空提示。我无法运行 bash 脚本。但我只做了几分钟的测试。

回答by dargaud

This is a solution using qdbus, see D-Bus documentation. The Konsole docsdoesn't say much about the interfaces used, so some experimenting is necessary. I've left comments in the code about the things I attempted but that didn't work.

这是使用 的解决方案qdbus,请参阅D-Bus 文档。在Konsole的文档没有说太多关于所使用的接口,因此一些试验是必要的。我在代码中留下了关于我尝试过但没有用的事情的评论。

This works in KDE 5.

这适用于 KDE 5。

#! /bin/bash
# Multi command start in various konsole tabs

# List of commands to run, with parameters, in quotes, space-separated; do not use quotes inside (see bash arrays)
COMMANDS=("/my/prog1 param" "/my/prog2 param2" "/my/prog3 param1 param2 param3")

# KDS=$KONSOLE_DBUS_SERVICE # This is a ref to current Konsole and only works in Konsole
# KDS=$(org.kde.konsole)    # This is found in some examples but is incomplete

qdbus >/tmp/q0              # Get the current list of konsoles
/usr/bin/konsole            # Launch a new konsole
# PID=$!                    # And get its PID - But for some reason this is off by a few
sleep 1
qdbus >/tmp/q1              # Get the new list of konsoles
# KDS=org.kde.konsole-$PID      
# KDS=org.kde.konsole       # Sometimes
KDS=$(diff /tmp/q{0,1} | grep konsole)  # Let's hope there's only one
#echo $KDS
KDS=${KDS:3}
echo $KDS

echo $KDS >/tmp/KDS
echo >>/tmp/KDS

qdbus $KDS >>/tmp/KDS || exit
echo >>/tmp/KDS

# See note https://docs.kde.org/trunk5/en/applications/konsole/scripting.html about using /Konsole
qdbus $KDS /Konsole >>/tmp/KDS
echo >>/tmp/KDS

FirstTime=1

for i in "${COMMANDS[@]}"
do 
    echo "Starting: $i"
    echo >>/tmp/KDS
    if [ $FirstTime -eq 1 ]
    then
        session=$(qdbus $KDS /Konsole currentSession)
        FirstTime=0
    else
        session=$(qdbus $KDS /Konsole newSession)
    fi
    echo $session >>/tmp/KDS

    # Test: Display possible actions
    qdbus $KDS /Sessions/${session} >>/tmp/KDS

    # Doesn't work well, maybe use setTabTitleFormat 0/1 instead
    # Title "0" appears to be the initial title, title "1" is the title used after commands are executed. 
    #qdbus $KDS /Sessions/${session} setTitle 0 $i
    #qdbus $KDS /Sessions/${session} setTitle 1 $i

    # The line break is necessary to commit the command. \n doesn't work
    qdbus $KDS /Sessions/${session} sendText "${i}
"

    # Optional: will ping when there's no more output in the window
    qdbus $KDS /Sessions/${session} setMonitorSilence true
done

Update 2016: the structure of qdbus has changed again. Here's an update of the above script (I left out the original since depending on your KDE version you may need one or the other):

2016 年更新:qdbus 的结构再次发生变化。这是上述脚本的更新(我省略了原始脚本,因为根据您的 KDE 版本,您可能需要其中一个):

#! /bin/bash
# Multi command start in various konsole tabs

# List of commands to run, with parameters, in quotes, space-separated; do not use quotes inside (see bash arrays)
COMMANDS=("echo 1" "echo 2" "echo 3")

# KDS=$KONSOLE_DBUS_SERVICE # This is the ref of the current konsole and only works in a konsole
# KDS=$(org.kde.konsole)    # This is found in some examples but is incomplete

qdbus >/tmp/q0              # Get the current list of konsoles
/usr/bin/konsole            # Launch a new konsole
sleep 1
qdbus >/tmp/q1              # Get the new list of konsoles
KDS=$(diff /tmp/q{0,1} | grep konsole)  # Let's hope there's only one
KDS=${KDS:3}
echo $KDS

echo $KDS >/tmp/KDS
echo >>/tmp/KDS

qdbus $KDS >>/tmp/KDS || exit
echo >>/tmp/KDS

# See note https://docs.kde.org/trunk5/en/applications/konsole/scripting.html about using /Konsole
qdbus $KDS /konsole >>/tmp/KDS
echo >>/tmp/KDS

FirstTime=1

for i in "${COMMANDS[@]}"
do 
    echo "Starting: $i"
    echo >>/tmp/KDS
    if [ $FirstTime -eq 1 ]
    then
        session=$(qdbus $KDS /Windows/1 currentSession)
        FirstTime=0
    else
        session=$(qdbus $KDS /Windows/1 newSession)
    fi
    echo $session >>/tmp/KDS

    # Test: Display possible actions
    qdbus $KDS /Sessions/${session} >>/tmp/KDS

    # The line break is necessary to commit the command. \n doesn't work
    qdbus $KDS /Sessions/${session} sendText "${i}
"

    # Optional: will ping when there's no more output in the window
    qdbus $KDS /Sessions/${session} setMonitorSilence true
done

回答by CaptainCrunch

I did some more digging and found and even more "subjectively" beautiful answer. Goal: start empty shell, music player and screen session running irssi in 3 different tabs in konsole:

我做了更多的挖掘,发现了更“主观”的美丽答案。目标:在 konsole 的 3 个不同选项卡中启动运行 irssi 的空壳、音乐播放器和屏幕会话:

  1. Create a simple, executable script file with:
  1. 使用以下命令创建一个简单的可执行脚本文件:

#!/bin/bash konsole --hold --new-tab & konsole --hold --new-tab -e $SHELL -c "/usr/bin/screen -DRS irssi-in-screen irssi" & konsole --hold --new-tab -e $SHELL -c "/usr/bin/ncmpcpp" &

#!/bin/bash konsole --hold --new-tab & konsole --hold --new-tab -e $SHELL -c "/usr/bin/screen -DRS irssi-in-screen irssi" & konsole --hold --new-tab -e $SHELL -c "/usr/bin/ncmpcpp" &

The clue is not to execute the the command directly but to call a shell, that can take in all arguments passed. $SHELL is set to /bin/bash. This "issue" is documented here:

线索不是直接执行命令,而是调用一个 shell,它可以接收所有传递的参数。$SHELL 设置为 /bin/bash。这个“问题”记录在这里:

https://docs.kde.org/stable5/en/applications/konsole/commonissues.html

https://docs.kde.org/stable5/en/applications/konsole/commonissues.html

Quote: " Konsole treats arguments after the -e option as one command and runs it directly, instead of parsing it and possibly dividing it into sub-commands for execution. This is different from xterm.

konsole -e "command1 ; command2" does not work

konsole -e $SHELL -c "command1 ; command2" works

引用:“ Konsole 将 -e 选项后的参数视为一个命令并直接运行它,而不是解析它并可能将其拆分为子命令执行。这与 xterm 不同。

konsole -e "command1 ; command2" does not work

konsole -e $SHELL -c "command1 ; command2" works