![是否有一个多选项卡终端接受开始运行每个选项卡一个命令?](https://linux22.com/image/202912/%E6%98%AF%E5%90%A6%E6%9C%89%E4%B8%80%E4%B8%AA%E5%A4%9A%E9%80%89%E9%A1%B9%E5%8D%A1%E7%BB%88%E7%AB%AF%E6%8E%A5%E5%8F%97%E5%BC%80%E5%A7%8B%E8%BF%90%E8%A1%8C%E6%AF%8F%E4%B8%AA%E9%80%89%E9%A1%B9%E5%8D%A1%E4%B8%80%E4%B8%AA%E5%91%BD%E4%BB%A4%EF%BC%9F.png)
我的目标是启动一个多终端环境:
- 一次轻松访问所有终端,就像按键盘键一样(我可以使用 yakuake 和 guake 做到这一点)
- 自定义命名选项卡,这样我就知道东西在哪里
- 使用预先执行的完整命令启动多选项卡终端,这样我就可以为每个选项卡终端提供完全设置的环境
我今天做什么?
我打开 guake,它使用的 RAM 比 yakuake 少。
我在 bash 上配置了一些按键绑定,因此我可以按 F5 之类的键并启动用于开发的基本环境,因此我仍然必须为每个特定终端按 F5 键。
我仍然需要在 5 个选项卡中的每一个上输入它们的具体命令。
我之前还给它们命名过,这样我就知道在哪个上运行什么特定命令。
研究:
- 多选项卡终端:yakuake、guake、先生xvt。
- 可重命名的选项卡:guake、yakuake,但我找不到 mrxvt 的方法。
- 我知道我们可以使用总线 使用 yakuake 可以做很多事情,比如更改选项卡名称。不确定瓜克。找不到使用 mrxvt 更改选项卡名称的方法。
间接方式(不需要是命令行,可以通过 qdbus)来实现最终结果也足够了,我可能只需要准备一些脚本。
使用最低 RAM 的应用程序 (mrxvt) 对于处理正在运行的其他应用程序也很重要,但如果我无法使用它来实现我的目标,那就没有意义了。
答案1
如果您正在寻找“多终端”环境,我建议不要寻找终端本身(终结器是一个不错的选择),而是检查终端多路复用器的路径,例如tmux
.
有了它,tmux
您将拥有更高级终端的所有优点,但您可以在任何终端中使用它,也可以在远程服务器中使用它。
tmux
还有一个优点是,如果您关闭终端,您的 TMUX 会话仍然存在,您可以恢复它,并且所有 vim 文件、o 程序和 ssh 会话仍在运行。
为了使用预定义的 TABS(tmux 中的窗口)配置多个终端并自动运行一些命令,tmux
有 tmuxinator (https://github.com/tmuxinator/tmuxinator) 和 tmuxp (https://tmuxp.git-pull.com/)
tmuxp 示例
session_name: 4-pane-split
start_directory: ./ # load session relative to config location (project root).
before_script: ./.tmuxp-before-script.sh
shell_command_before:
- '[ -f .venv/bin/activate ] && source .venv/bin/activate && reset'
windows:
- window_name: dev window
layout: tiled
shell_command_before:
- cd ~/ # run as a first command in all panes
panes:
- shell_command: # pane no. 1
- cd /var/log # run multiple commands in this pane
- ls -al | grep \.log
- echo second pane # pane no. 2
- echo third pane # pane no. 3
- echo forth pane # pane no. 4
- window_name: docs
layout: main-horizontal
options:
main-pane-height: 35
start_directory: docs/
panes:
- focus: true
- pane
- make serve
- make SPHINXBUILD='poetry run sphinx-build' watch
一些链接:
答案2
如果你的 guake 版本 >= 3.8.5-1,你将需要这个:(
cat >>bashAutoCmdOnStart.sh
并将此命令添加到 ~/.bashrc 的末尾)
#!/bin/bash
strFlBase="/tmp/`basename "$0"`."
if [[ "$1" == "--getExecFileForUID" ]];then
shift
echo "Showing id for current term on guake:" >&2
echo "${strFlBase}$1"
exit 0
fi
#TODO for yakuake too
if [[ -z "$GUAKE_TAB_UUID" ]];then declare -p GUAKE_TAB_UUID;exit 0;fi
: ${bBashAutoCmdOnStart:=true} #help
if ! $bBashAutoCmdOnStart;then ps;pwd;declare -p bBashAutoCmdOnStart;exit 0;fi
ps;pwd
strFlExec="${strFlBase}$GUAKE_TAB_UUID"
nCount=0
while true;do
echo -ne "$SECONDS: wating terminal auto cmd ($strFlExec). Hit any key for prompt.\r";
#TODO test if terminal is interactive, I think `read` is failing when guake loads initially on pc restart
if read -t 3 -n 1;then break;fi;
if [[ -f "$strFlExec" ]];then
echo "EXECUTING file contents '$strFlExec' here:"
cat "$strFlExec"
strRunFl="${strFlExec}.Running.${nCount}"
mv -v "$strFlExec" "$strRunFl"
#chmod +x "${strFlExec}.Running"
source "${strRunFl}"
ps;pwd
#bash -c "${strFlExec}.Running;ps;pwd;bash"
((nCount++))&&:
fi
done
ps;pwd
bBashAutoCmdOnStart=false bash
运行示例:guakeAutoEnv.sh [-x] -ID_CMD workA ls 123 -ID_CMD workB ls 456 789
#-x 是从 xterm 运行它(而不是当前的 guake 选项卡)
cat >>guakeAutoEnv.sh
#!/bin/bash
nWId="`wmctrl -l |grep "Guake!" |awk '{print $1}'`";declare -p nWId
: ${bUseBashAutoCmdMode:=false}
if ! qdbus org.guake3.RemoteControl /org/guake3/RemoteControl |grep execute_command_by_uuid;then
#echoc --alert "@{-n}execute_command_by_uuid was @nremoved@{-n}, therefore this script wont work that way anymore :(";
bUseBashAutoCmdMode=true
fi
if [[ -z "${1-}" ]] || [[ "${1}" == --help ]];then
#help Usage ex.: [-x] -ID_CMD coolName ls 123 -ID_CMD funnyName ls 456 789
egrep "[#]help" $0 |sed -r -e 's"^[[:space:]]*""' -e 's"[[:space:]]*$""' >/dev/stderr
exit 0
fi
export bUsingXtermToRun=false;
if [[ "${1-}" == "-x" ]];then
bUsingXtermToRun=true;
shift&&:;
(xterm -e $0 "$@"&disown) #w/o -x now
exit
fi
set -Eeu
# depends on
while ! pgrep -fa "`which guake`";do echo " >>>>> WARN:WaitingGuakeStart";sleep 3;done #this line checks for a running guake
while ! qdbus org.guake3.RemoteControl;do echo " >>>>> WARN:WaitingGuakeDBus";sleep 3;done #this line checks for a listening guake
function FUNCexecEcho() {
echo " EXEC: $@" >/dev/stderr
"$@"
};export -f FUNCexecEcho
function FUNCgrc() {
FUNCexecEcho qdbus org.guake3.RemoteControl /org/guake3/RemoteControl org.guake3.RemoteControl."$@"
}
# read parameters
acmd=()
astrIdList=()
astrOptList=()
iIdIndex=0
function FUNCchkCmd() { if [[ "${1:0:7}" == "-ID_CMD" ]];then return 0;else return 1;fi; } # <"$1">
while ! ${1+false};do
if FUNCchkCmd "${1}";then
echo " >>> NEW ID_CMD iIdIndex=$iIdIndex:"
astrOptList+=("${1#-ID_CMD}");shift
astrIdList+=("$1");shift;
continue;
fi
acmd+=("$1");shift
if ${1+false} || FUNCchkCmd "${1}";then
echo "astrOptList[$iIdIndex]='${astrOptList[$iIdIndex]}'"
echo "astrIdList[$iIdIndex]='${astrIdList[$iIdIndex]}'"
eval 'aIdCmd'${iIdIndex}'=("${acmd[@]}")';declare -p aIdCmd${iIdIndex}
((iIdIndex++))&&:
acmd=()
fi
done
declare -p astrIdList
# detect existing tabs
iTabCount="`FUNCgrc get_tab_count`"
iCurrentTabIndex="`FUNCgrc get_selected_tab`"
strCurrentTabUUID="`FUNCgrc get_selected_uuidtab`"
astrTabUUIDList=()
astrTabNameList=()
for((i=0;i<iTabCount;i++));do
echo
declare -p i
strTabName="`FUNCgrc get_tab_name $i`";declare -p strTabName
if((i==iCurrentTabIndex));then echo " CURRENT TAB";fi
FUNCgrc select_tab $i
astrTabUUIDList+=("`FUNCgrc get_selected_uuidtab`")
astrTabNameList+=("`FUNCgrc get_tab_name $i`")
done
FUNCgrc select_tab $iCurrentTabIndex # this first is reached fast so the user has not to wait a long time
: ${bGoBackToInitialTab:=false} #help will re-select select the tab this script was run on at the end
declare -p astrTabUUIDList |tr '[' '\n'
declare -p astrTabNameList |tr '[' '\n'
function FUNCrunCmd() {
strOpts="$1";shift
strTabName="$1";shift
strTabUUID="$1";shift
iIdCmd="$1";shift
aOptList=();if [[ -n "$strOpts" ]];then aOptList+=(`echo "$strOpts" |sed -r 's@(.)@\1 @g'`);fi
echo
echo " TabName: ${strTabName}, CMDOPTION(s): ${aOptList[@]} "
eval 'acmdRun=("${aIdCmd'${iIdCmd}'[@]}")';declare -p acmdRun
bSubShell=true
bForceCurrentTab=false
bRetryOnError=false
for strOpt in "${aOptList[@]}";do
case "$strOpt" in
n)bSubShell=false;; #help CMDOPTION: The default is to run using `bash -c '$cmd'`. To disable this, use like ex.: `-ID_CMDn cd ComePathToApplyAtCurrentShell`
c)bForceCurrentTab=true;if ! $bUsingXtermToRun;then echo " >>>>> WARNING <<<<< 'c' CMDOPTION is more reliable using '-x' option!!!";fi;; #help CMDOPTION: The tab name will only be ignored (but only if it does not exist ex.: "IgnoredCurrentTabName") and the current tab running this script will be used. This option is more reliable with -x option.
r)bRetryOnError=true;; #help CMDOPTION: if the command fails returning any error, it will retry running it after 3s.
*)$0 --help;echo "ERROR: invalid option '$strOpt' for strTabName='${strTabName}' and command: ${acmdRun[*]}";exit 1;;
esac
done
# create and run a tmp script
strFl="`tempfile`";chmod +x "$strFl"
echo '#!/bin/bash
cat $0; # review the command
'"$(declare -p acmdRun)"'; # '"${acmdRun[@]}"'
echo >'"${strFl}.started"'
if '$bRetryOnError';then
while ! "${acmdRun[@]}";do echo "failed error $?";sleep 3;done
else
"${acmdRun[@]}";
fi
' >"$strFl"
while true;do
bForceIgnoreStart=false
if $bUseBashAutoCmdMode && $bSubShell;then
bSubShell=false
echo "WARN: disabling subshell mode to let bUseBashAutoCmdMode work for now." >&2
fi
if $bSubShell;then
strCmd="bash -c ${strFl}"
else
# can the command passed thru qdbus param cause some problem if it is too complex? `bash -c` above is to prevent that.
strCmd="`declare -p acmdRun`;"'"${acmdRun[@]}"'
bForceIgnoreStart=true
fi
if $bUseBashAutoCmdMode;then
#echoc --alert "@{-n}execute_command_by_uuid was @nremoved@{-n}, therefore this script wont work that way anymore :(";
#exit 1
strExecFile="`bashAutoCmdOnStart.sh --getExecFileForUID "$strTabUUID"`"
echo "$strCmd" >>"$strExecFile"
while [[ -f "$strExecFile" ]];do
#echo "INFO: Waiting '$strExecFile' be consumed..."
read -p "(`date`)INFO: Waiting '$strExecFile' be consumed... skip waiting? (y/...)`echo -ne "\r"`" -t 1 -n 1 strResp&&:;if [[ "$strResp" == "y" ]];then break;fi
sleep 1
done
else
# this is for old guake version
if [[ "$strTabUUID" == "USE_CURRENT_TAB" ]];then
FUNCgrc execute_command "$strCmd"
else
FUNCgrc execute_command_by_uuid "$strTabUUID" "$strCmd"
fi
fi
sleep 1 #wait it start running
: ${bWaitStart:=true} #help wait the command start running before sending the next one
bForceRetry=false
bKeepTmpExecFile=false
bReallyWaitStart=$bWaitStart;if $bForceIgnoreStart;then bReallyWaitStart=false;fi
if $bReallyWaitStart;then
if $bForceCurrentTab || [[ "$strCurrentTabUUID" == "$strTabUUID" ]];then
echo " >>>>>>>>>>>> "
echo " >>>>> ATTENTION!!! this command '${acmdRun[*]}' was requested to be run on this current tab [$iCurrentTabIndex]'${strTabName}'($strCurrentTabUUID), so this script will not wait for the command to start and the related temporary exec file '${strFl}' will be kept."
echo " >>>>>>>>>>>> "
bKeepTmpExecFile=true
else
nW=0
while [[ ! -f "${strFl}.started" ]];do
echo " >>>>>>>>>>>> "
echo " > `date`: Waiting ($((nW++))) the command start running:"
echo " > TAB: strTabName='${strTabName}',strTabUUID='$strTabUUID'"
echo " > CMD$iIdCmd: '${acmdRun[*]}'"
echo " > It may fail if there was some command typed and not executed yet there."
echo " > It may fail if there happened an input request there."
echo " > So you better check if no problem happened!"
echo " > But if no problem happened and you force a retry, it will be buffered and will execute again."
echo " > Force retry run now? (y/...)"
echo " >>>>>>>>>>>> "
strResp="$(read -t 3 -n 1 strResp;echo "$strResp")";echo #the script was stopping here... why did I have to subshell?
if [[ "$strResp" == "y" ]];then echo " >>>>> Retrying";bForceRetry=true;break;fi
done
fi
fi
if ! $bForceRetry;then break;fi
done
: ${bRmTmpFiles:=false} #help delete temporary files
if $bRmTmpFiles && ! $bKeepTmpExecFile;then rm -v "${strFl}" "${strFl}.started";fi
};export -f FUNCrunCmd
# run the commands
for iId in "${!astrIdList[@]}";do
declare -p iId
echo " Id: ${astrIdList[iId]}"
bFound=false
for iTab in "${!astrTabNameList[@]}";do
strOpt="${astrOptList[iId]}"
bRunOnThisTab=false
if [[ "${astrTabNameList[iTab]}" == "${astrIdList[iId]}" ]];then bRunOnThisTab=true;fi
if ((iTab==iCurrentTabIndex)) && [[ "$strOpt" =~ .*c.* ]];then bRunOnThisTab=true;fi
if $bRunOnThisTab;then
FUNCrunCmd "$strOpt" "${astrIdList[iId]}" "${astrTabUUIDList[iTab]}" $iId
bFound=true
break
fi
done
: ${bAddTab:=true} #help if it doesnt exist yet
if ! $bFound;then
if $bAddTab;then
FUNCgrc add_tab "$HOME"
FUNCgrc rename_current_tab "${astrIdList[iId]}"
astrTabNameList+=("${astrIdList[iId]}")
astrTabUUIDList+=("`FUNCgrc get_selected_uuidtab`")
FUNCrunCmd "${astrOptList[iId]}" "${astrIdList[iId]}" "USE_CURRENT_TAB" $iId
else
echo " >>>>>>>>>>>> "
echo " >>>>> WARNING: could not find a matching tab name for '${astrIdList[iId]}'"
echo " >>>>>>>>>>>> "
fi
fi
done #for iId in "${!astrIdList[@]}";do
if $bGoBackToInitialTab;then FUNCgrc select_tab $iCurrentTabIndex;fi