如何启动具有多个选项卡 运行 各种程序的 KDE 控制台?

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

我知道如何启动带有一个可执行文件 运行 的 Konsole,并在程序结束后让 Konsole 保持打开状态。我可以使用 .desktop 文件并更改其中的一些选项来做到这一点。

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

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

这是一个使用 qdbus 的解决方案,请参阅 D-Bus documentationKonsole docs 并没有说明所使用的接口,因此有必要进行一些试验。我在代码中留下了关于我尝试但没有奏效的事情的评论。

这适用于 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

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

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

  1. 像这样创建一个配置制表符的文件并将其命名为“制表符”:
   title: %n;; command: /usr/bin/htop
   title: %n;; command: /usr/bin/ncmpcpp

(完整文档位于 https://docs.kde.org/stable5/en/konsole/konsole/command-line-options.html。 调用的命令二进制文件是示例。 "%n" 将完全按照命令命名选项卡)

  1. 这样执行:

    konsole --tabs-from-file path_to_tabs_file/tabs

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

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

  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" &

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

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

qdbus 上述解决方案对我不起作用,因为可阻塞调用 /usr/bin/konsole,所以我对其进行了一些升级。我正在使用 ZSH,所以请更改您的 shebang。

#! /bin/zsh
# 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=("vi" "nano")
# Geting length of the COMMANDS array
len_arr=${#COMMANDS[@]}

# Simple /usr/bin/konsole block this script, no work for me. So use qdbus to run konsole
qdbus org.kde.klauncher5 /KLauncher exec_blind "/usr/bin/konsole" "/home/$USER"
# Wait until konsole was run up completely. 1s for me
sleep 1s
# get the last added konsole and save it in $KDS variable
qdbus | grep konsole | tail -1 | { read KDS }
# loop the array with commands . 
for (( i=1; i<=$len_arr; i++ ))
do
 if [ $i -gt 1 ]
 then
     # for all commands beside first getting the number of the new konsole tab
     session=$(qdbus $KDS /Windows/1 newSession)
     
 else
     # get the number of the current console tab
     session=$(qdbus $KDS /Windows/1 currentSession)
 fi
 # run current command in tab
 qdbus $KDS /Sessions/${session} runCommand "${COMMANDS[$i]}"
 
 # Silence if you need. I'm not using it.
 # Optional: will ping when there's no more output in the window
 # qdbus $KDS /Sessions/${session} setMonitorSilence true
done