如何启动一个KDE konsole与多个选项卡运行各种prog?

时间:2015-09-25 09:40:53

标签: bash dbus kde konsole

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

但是我想更进一步,推出一个打开多个标签的KDE konsole,每个标签都运行一个特定的程序,当程序完成时它保持打开并给你一个提示。

还没有Konsole的手册页,所以我甚至不知道它可以采取什么选择。还是一些d-bus电话? 感谢

4 个答案:

答案 0 :(得分:6)

这是使用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 :(得分:4)

在所接受的解决方案中,谁曾经看过美女,希望不在软件开发中:)这个必须是一个单行或必须提交错误报告。每个其他公共终端都有此选项。我做了一些研究,“差不多一个班轮解决方案”是这样的:

  1. 创建一个配置您的制表符的文件,并命名为“tabs”:

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

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

  2. (这是完整的文档: https://docs.kde.org/stable5/en/applications/konsole/command-line-options.html 被叫命令二进制文件就是例子。 “%n”将命名与命令完全相同)

    1. 执行它:

      konsole --tabs-from-file path_to_tabs_file / tabs

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

答案 2 :(得分:2)

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

  1. 使用以下命令创建一个简单的可执行脚本文件:
  2. #!/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。这"问题"记录在这里:

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

      

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

    konsole -e "command1 ; command2" does not work
    
    konsole -e $SHELL -c "command1 ; command2" works
    

答案 3 :(得分:0)

上面的

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