打开具有多个选项卡的终端并执行为每个选项卡唯一修改 PS1 变量的应用程序

Gab*_*les 5 command-line bash scripts gnome-terminal tabs

我正在尝试做的事情:

\n\n
    \n
  1. 编写一个脚本来打开 3 个选项卡。
  2. \n
  3. cd进入每个选项卡中的不同文件夹(即:运行唯一的命令)。
  4. \n
  5. PS1通过修改其局部变量使每个选项卡具有唯一的标题
  6. \n
  7. 确保脚本运行后每个选项卡保持打开状态
  8. \n
\n\n

我想要编写此脚本,以便我可以单击桌面上的脚本并让它打开终端,就像我想要的日常开发环境一样。

\n\n

描述:

\n\n

我有这个脚本来尝试打开 3 个终端选项卡,并在选项卡中运行独特的命令:

\n\n

open_tabs.sh

\n\n\n\n
#!/bin/bash\n\ngnome-terminal --tab -- bash -ic "set-title title 1; exec bash"\ngnome-terminal --tab -- bash -ic "cd ~; set-title title 2; exec bash"\ngnome-terminal --tab\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我使用 运行它时./open_tabs.sh,它会打开 3 个新选项卡,但不幸的set-title是无法设置选项卡标题!看来该PS1变量没有随着我的set-title调用而保持设置状态。根据此答案及其下面的评论,可以exec bash保持选项卡打开。

\n\n

我已经像这样set-title定义了一个函数~/.bashrc。其目的是在任何终端窗口的顶部设置标题字符串。当我手动使用它时它工作得很好。例如:set-title hey how are you?将输入“嘿,你好吗?” 在我的终端窗口的顶部。

\n\n
# From: https://unix.stackexchange.com/questions/177572/how-to-rename-terminal-tab-title-in-gnome-terminal/566383#566383\nset-title() {\n    # If the length of string stored in variable `PS1_BAK` is zero...\n    # - See `man test` to know that `-z` means "the length of STRING is zero"\n    if [[ -z "$PS1_BAK" ]]; then\n        # Back up your current Bash Prompt String 1 (`PS1`) into a global backup variable `PS1_BAK`\n        PS1_BAK=$PS1 \n    fi\n\n    # Set the title escape sequence string with this format: `\\[\\e]2;new title\\a\\]`\n    # - See: https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Customizing_the_terminal_window_title\n    TITLE="\\[\\e]2;$@\\a\\]"\n    # Now append the escaped title string to the end of your original `PS1` string (`PS1_BAK`), and set your\n    # new `PS1` string to this new value\n    PS1=${PS1_BAK}${TITLE}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

如何解决此问题,以便每个选项卡运行一个命令,通过修改其PS1变量来设置其标题,并保持打开状态?

\n\n

请注意,gnome-terminal已弃用它的--title--command选项,因此有这些解决方法。

\n\n

有关的:

\n\n
    \n
  1. bash:打开 gnome-terminal 选项卡时,在 `bash -c` 命令中调用 ~/.bashrc 文件中定义的函数时,“找不到命令”
  2. \n
  3. https://unix.stackexchange.com/questions/177572/how-to-rename-terminal-tab-title-in-gnome-terminal/566383#566383
  4. \n
  5. 打开具有多个选项卡的终端并执行应用程序<== 这就是我真正想要解决的问题,但是gnome-terminal\'s --command( -e) 选项现已弃用!

    \n\n
    # Option \xe2\x80\x9c--command\xe2\x80\x9d is deprecated and might be removed in a later version of gnome-terminal.\n# Use \xe2\x80\x9c-- \xe2\x80\x9d to terminate the options and put the command line to execute after it.\n
    Run Code Online (Sandbox Code Playgroud)
  6. \n
  7. 如何在不关闭终端的情况下运行脚本?

  8. \n
\n

Gab*_*les 4

与大多数编程的情况一样,解决问题非常困难。我必须学习一些关于 Bash 变量的知识,以及如何使用exportand source(或 POSIX 点运算符,.),以及 bash 如何加载,以及交互式-ibash 模式是什么,等等。

我发现man bash并且man test也很有用。以下是如何做我想做的事情,即:

  1. 编写一个脚本来打开 3 个选项卡。
  2. cd 到每个选项卡中的不同文件夹(即:运行唯一的命令)。
  3. 通过修改本地 PS1 变量让每个选项卡具有唯一的标题
  4. 确保脚本运行后每个选项卡保持打开状态

1、将其添加到文件的底部~/.bashrc

# 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
Run Code Online (Sandbox Code Playgroud)

2、创建此脚本以打开 3 个独特的选项卡,其中包含 3 个独特的 cd 命令和 3 个独特的标题:

open_tabs.sh

gnome-terminal --tab -- bash -ic "export TITLE_DEFAULT='title 1'; cd ..; exec bash;"
gnome-terminal --tab -- bash -ic "export TITLE_DEFAULT='title 2'; cd ../..; exec bash;"
gnome-terminal --tab -- bash -ic "export TITLE_DEFAULT='title 3'; cd ../../..; exec bash;"
Run Code Online (Sandbox Code Playgroud)

现在打开终端并运行open_tabs.sh脚本:

./open_tabs.sh
Run Code Online (Sandbox Code Playgroud)

瞧!太神奇了!这 3 个新选项卡现在显示在我的终端顶部,每个选项卡都执行了cd我设置的正确命令,并且每个选项卡都有我设置的正确标题!

在此输入图像描述

这将全部放入我的点文件项目中:https ://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles 。

完整和最终的解决方案:请参阅此处:使用多个选项卡打开终端并执行应用程序