use*_*001 21 command-line bash scripts
我是 linux shell 脚本的新手。我想编写一个 shell 脚本,它将打开带有多个选项卡的终端;它应该在每个选项卡中运行 rtsp 客户端应用程序。
为此,我已经在这个论坛上解决了问题,并尝试像下面这样编码,
tab="--tab-with-profile=Default -e "
cmd="java RunRTSPClient"
for i in 1 2 3 4 5
do
#
foo="$foo $tab $cmd"
done
gnome-terminal $foo
exit 0
Run Code Online (Sandbox Code Playgroud)
它正在运行并打开带有选项卡的终端窗口,但它会突然关闭。我没有收到任何错误。
Rad*_*anu 14
使用此脚本变体来执行您想要的操作:
#!/bin/bash
tab="--tab-with-profile=Default"
cmd="bash -c 'java RunRTSPClient';bash"
foo=""
for i in 1 2 3 4 5; do
foo+=($tab -e "$cmd")
done
gnome-terminal "${foo[@]}"
exit 0
Run Code Online (Sandbox Code Playgroud)
通常,这样的脚本:
#!/bin/bash
tab="--tab"
cmd="bash -c '<command-line_or_script>';bash"
foo=""
for i in 1 2 ... n; do
foo+=($tab -e "$cmd")
done
gnome-terminal "${foo[@]}"
exit 0
Run Code Online (Sandbox Code Playgroud)
将打开一个新终端,其中有 n 个选项卡<command-line_or_script>在每个选项卡中执行。例如,当您想在特定路径上使用解释器打开带有某些选项卡的终端(cd /path在上述脚本中使用)时,这可能非常有用。
我已经测试了这些脚本并且它们可以工作。
Gab*_*les 14
我想出了我自己的答案。我认为这是一个更好的方法,因为:
..., ${}, -e, 或@请注意,; $SHELL每个 gnome-terminal 命令末尾的 是保持终端窗口打开的原因。否则它会立即关闭。
gnome-terminal,例如来自 Ubuntu 14.04 的 。gnome-terminal禁用--title选项的某个或3.16.2版本(见伊万Kozik评论后,这个答案下,看看我的回答如下我的意见),然而,虽然下面的脚本的其余部分确实仍然在现代版本的工作gnome-terminalUbuntu和,设置每个选项卡的标题--title不。请参阅下面的新版本,了解适用于任何地方的替代方案。旧代码(像这样设置选项卡标题在 Ubuntu 16 或 18 或更高版本中不再起作用,--command不幸的是,该选项现在也已弃用):
title1="tab 1"
title2="tab 2"
title3="tab 3"
cmd1="cd /etc"
cmd2="cd ~/Documents"
cmd3="cd /usr/local"
gnome-terminal --tab --title="$title1" --command="bash -c '$cmd1; $SHELL'" \
--tab --title="$title2" --command="bash -c '$cmd2; $SHELL'" \
--tab --title="$title3" --command="bash -c '$cmd3; $SHELL'"
Run Code Online (Sandbox Code Playgroud)
新代码——在gnome-terminal和terminator终端的所有版本中都能完美运行(如果需要,可以扩展到更多终端),并且已经在 Ubuntu 14.04、16.04、18.04 和 20.04 中进行了测试。您可以在我的 dotfiles 项目中的个人“~/.bash_aliases”和“~/.bash_aliases_private文件”中找到最新和最好的版本。阅读自述文件,并在“~/.bash_aliases”文件中搜索名为“CUSTOM TERMINAL TABS & TITLES”。在“~/.bash_aliases_private”文件中,搜索名为“CONFIGURATION FOR Bash function”的部分。另见“open_programming_tools.sh”gs_open_default_tabs 桌面启动器文件,这两个文件都用于根据您的配置快速打开带有所有选项卡的终端。
在这一点之后的所有以下信息都应该有效,但与我在上面的 repo 中发现的最新更改不同步。在那里查看我的最新技术和脚本。
像下面这样设置选项卡标题现在可以在所有版本的 gnome-terminal 中工作,所以它在 Ubuntu 16、18 和 20 及更高版本中工作正常!
有关更多详细信息和进一步了解,请参见此处和此答案底部的参考资料:https : //unix.stackexchange.com/questions/177572/how-to-rename-terminal-tab-title-in-gnome -终端/566383#566383
请注意,不幸的是,gnome-terminal --command( -e) 和--title选项现在已被弃用,因此这是一个困难的解决方法。如果我gnome-terminal使用命令行中已弃用的选项之一进行调用,则会收到以下警告:
# Option “--command” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
Run Code Online (Sandbox Code Playgroud)
我将使用一个有趣的自定义set-title函数,其中包含一些特殊用途的exported 变量和 ~/.bashrc 文件,因为它会在每次-i打开交互式 ( ) bash shell时自动获取。
~/.bashrc文件的底部:根据需要更新DEFAULT_TABS_TITLE和DEFAULT_TABS_CMD变量。
# Function to allow a user to arbitrarily set the terminal title to anything
# Example: `set-title this is title 1`
set-title() {
# Set the PS1 title escape sequence; see "Customizing the terminal window title" here:
# https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Customizing_the_terminal_window_title
TITLE="\[\e]2;$@\a\]"
PS1=${PS1_BAK}${TITLE}
}
# Back up original PS1 Prompt 1 string when ~/.bashrc is first sourced upon bash opening
if [[ -z "$PS1_BAK" ]]; then # If length of this str is zero (see `man test`)
PS1_BAK=$PS1
fi
# Set the title to a user-specified value if and only if TITLE_DEFAULT has been previously set and
# exported by the user. This can be accomplished as follows:
# export TITLE_DEFAULT="my title"
# . ~/.bashrc
# Note that sourcing the ~/.bashrc file is done automatically by bash each time you open a new bash
# terminal, so long as it is an interactive (use `bash -i` if calling bash directly) type terminal
if [[ -n "$TITLE_DEFAULT" ]]; then # If length of this is NONzero (see `man test`)
set-title "$TITLE_DEFAULT"
fi
DEFAULT_TABS_TITLE1="tab 1"
DEFAULT_TABS_TITLE2="tab 2"
DEFAULT_TABS_TITLE3="tab 3"
DEFAULT_TABS_CMD1="cd /etc"
DEFAULT_TABS_CMD2="cd ~/Documents"
DEFAULT_TABS_CMD3="cd '$HOME/temp/test folder'" # Use quotes like this if there are spaces in the path
open_default_tabs() {
gnome-terminal --tab -- bash -ic "export TITLE_DEFAULT='$DEFAULT_TABS_TITLE1'; $DEFAULT_TABS_CMD1; exec bash;"
gnome-terminal --tab -- bash -ic "export TITLE_DEFAULT='$DEFAULT_TABS_TITLE2'; $DEFAULT_TABS_CMD2; exec bash;"
gnome-terminal --tab -- bash -ic "export TITLE_DEFAULT='$DEFAULT_TABS_TITLE3'; $DEFAULT_TABS_CMD3; exec bash;"
}
# If length of this is NONzero
if [[ -n "$OPEN_DEFAULT_TABS" ]]; then
OPEN_DEFAULT_TABS= # reset to an empty string so this only happens ONCE
open_default_tabs
exit 0 # close the calling process so only the "default tabs" are left open
fi
Run Code Online (Sandbox Code Playgroud)
open_default_tabs从任何终端调用该函数。由于您刚刚更新了 ~/.bashrc 文件,因此您需要先让您的终端知道它,然后它才能让您访问您的新功能和特性。您需要“重新获取”您的 ~/.bashrc 文件。因此,关闭并重新打开您的终端,或者调用. ~/.bashrc或source ~/.bashrc重新获取您的 ~/.bashrc 文件。然后,只需拨打电话open_default_tabs,您就会神奇地打开所有您想要打开和命名的标签,并将其cd编入您设置的目录中,就像这样!
open_tabs.sh:
# Export this variable so your ~/.bashrc file will see it and do the magic.
export OPEN_DEFAULT_TABS=true
# Open a new terminal window, which by default also sources your ~/.bashrc file again,
# thereby kicking off the process since you set the `OPEN_DEFAULT_TABS` variable just above.
gnome-terminal
OPEN_DEFAULT_TABS= # set this variable back to an empty string so it's no longer in force
unset OPEN_DEFAULT_TABS # unexport it
Run Code Online (Sandbox Code Playgroud)
open_tabs.desktop:
[Desktop Entry]
Name=Open Tabs
Name[en_US]=Open Tabs
Comment=
Exec=/path/to/open_tabs.sh
Icon=terminal
Terminal=false
Type=Application
StartupNotify=true
Run Code Online (Sandbox Code Playgroud)
然后做:
chmod +x open_tabs.sh
chmod +x open_tabs.desktop
Run Code Online (Sandbox Code Playgroud)
将 open_tabs.desktop 放在桌面上并双击它。
瞧!太神奇了!您将获得一个带有 3 个标题选项卡的新终端窗口,每个选项卡都位于您根据您在 ~/.bashrc 文件中配置的命令设置的正确目录中。
请注意,此代码以及更多有用的配置设置和脚本已放入我的 dotfile 项目:https : //github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles。这段代码在这里:
致@egmont:
回答您的问题:这是我执行此操作时得到的结果:gnome-terminal --tab --title abc -e 'sleep 10' --tab --title def -e 'sleep 10'。
$ gnome-terminal --tab --title abc -e 'sleep 10' --tab --title def -e 'sleep 10'
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
Run Code Online (Sandbox Code Playgroud)
然而,DOES OPEN UP 2新标签即刻,与标题设置为ABC和DEF。然而,大约 10 秒后,选项卡会自动关闭并且不会保持打开状态。
| 归档时间: |
|
| 查看次数: |
38035 次 |
| 最近记录: |