我做了更多的挖掘,发现了更“主观”的美丽答案。目标:在 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。这个“问题”记录在这里:
引用:“ Konsole 将 -e 选项后的参数视为一个命令并直接运行它,而不是解析它并可能将其拆分为子命令执行。这与 xterm 不同。
Run Code Online (Sandbox Code Playgroud)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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
希望在公认的解决方案中看到美的人不会在软件开发中:)这必须是一条线,否则必须提交错误报告。其他所有公共终端都有此选项。我做了一些研究,“几乎一个内衬解决方案”是这样的:
创建一个像这样配置制表符的文件,并命名为“ tabs”:
标题:%n ;; 命令:/ usr / bin / htop
标题:%n ;; 命令:/ usr / bin / ncmpcpp
(以下是完整的文档:https : //docs.kde.org/stable5/zh-CN/applications/konsole/command-line-options.html 所调用的命令二进制文件是示例。“%n”将为选项卡命名,就像命令)
像这样执行它:
konsole --tabs-from-file path_to_tabs_file / tabs
结果:一个带有3个选项卡的新konsole窗口,运行定义的二进制文件和一个空提示。我无法运行bash脚本。但是我只做了几分钟的测试。
归档时间: |
|
查看次数: |
5497 次 |
最近记录: |