我知道如何启动一个在其中运行一个可执行文件的 Konsole,并在程序结束后使 Konsole 保持打开状态。我可以使用
.desktop
文件来完成此操作并更改其中的一些选项。
但我想更进一步,启动一个打开多个选项卡的 KDE konsole,每个选项卡运行一个特定的程序,并且当程序完成时它保持打开状态并给您一个提示。
Konsole 没有手册页,所以我什至不知道它可以采用哪些选项。或者一些 d-bus 调用? 谢谢
在已接受的解决方案中看到美感的人希望不是在软件开发中:)这必须是一个简单的行,否则必须提交错误报告。所有其他常见终端都有此选项。我做了一些研究,“几乎一个衬垫解决方案”是这样的:
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”将按照命令命名选项卡)
像这样执行:
konsole --tabs-from-file path_to_tabs_file/tabs
结果:一个带有 3 个选项卡的新 konsole 窗口,运行定义的二进制文件和一个空提示符。我无法运行 bash 脚本。但我只做了几分钟的测试。
我做了一些更多的挖掘,发现了甚至更“主观”的美丽答案。目标:启动空 shell、音乐播放器和屏幕会话,在 konsole 的 3 个不同选项卡中运行 irssi:
#!/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:
引用:“Konsole 将 -e 选项后面的参数视为一个命令 并直接运行它,而不是解析它并可能分割它 转化为子命令来执行。这与 xterm 不同。
konsole -e "command1 ; command2" does not work konsole -e $SHELL -c "command1 ; command2" works
这是使用
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
#!/bin/bash
konsole &
KPID=$!
sleep 0.1
#
# Run smth
#
qdbus org.kde.konsole-$KPID /Sessions/1 runCommand 'pwd'
qdbus org.kde.konsole-$KPID /Sessions/2 runCommand 'ls'
#
# Tune konsole a bit
#
qdbus org.kde.konsole-$KPID /Sessions/1 setTitle 1 'I love pwd!'
qdbus org.kde.konsole-$KPID /Sessions/2 setTitle 1 'And I love ls!'
qdbus org.kde.konsole-$KPID /Windows/1 setCurrentSession 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