21

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

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

Konsole 没有手册页,所以我什至不知道它可以采用哪些选项。还是一些 d-bus 呼叫?谢谢

4

4 回答 4

12

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

  1. 创建一个像这样配置制表符的文件,并将其命名为“tabs”:
   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

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

于 2018-02-14T09:57:55.113 回答
11

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

  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。这个“问题”记录在这里

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

konsole -e "command1 ; command2" does not work

konsole -e $SHELL -c "command1 ; command2" works
于 2018-02-14T11:09:20.197 回答
5

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

这适用于 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
于 2015-11-03T11:16:43.343 回答
-1

上面的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
 
于 2020-10-02T09:10:52.343 回答