AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1209780
Accepted
Gabriel Staples
Gabriel Staples
Asked: 2020-02-12 09:28:30 +0800 CST2020-02-12 09:28:30 +0800 CST 2020-02-12 09:28:30 +0800 CST

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

  • 772

我正在尝试做的事情:

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

我想要这个脚本,所以我可以单击我的桌面上的脚本并让它打开终端,因为我想要我的日常开发环境。

描述:

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

open_tabs.sh

#!/bin/bash

gnome-terminal --tab -- bash -ic "set-title title 1; exec bash"
gnome-terminal --tab -- bash -ic "cd ~; set-title title 2; exec bash"
gnome-terminal --tab

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

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

# From: https://unix.stackexchange.com/questions/177572/how-to-rename-terminal-tab-title-in-gnome-terminal/566383#566383
set-title() {
    # If the length of string stored in variable `PS1_BAK` is zero...
    # - See `man test` to know that `-z` means "the length of STRING is zero"
    if [[ -z "$PS1_BAK" ]]; then
        # Back up your current Bash Prompt String 1 (`PS1`) into a global backup variable `PS1_BAK`
        PS1_BAK=$PS1 
    fi

    # Set the title escape sequence string with this format: `\[\e]2;new title\a\]`
    # - See: https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Customizing_the_terminal_window_title
    TITLE="\[\e]2;$@\a\]"
    # Now append the escaped title string to the end of your original `PS1` string (`PS1_BAK`), and set your
    # new `PS1` string to this new value
    PS1=${PS1_BAK}${TITLE}
}

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

请注意,gnome-terminal已弃用其--title和--command选项,因此这些变通方法。

有关的:

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

    # 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.
    
  4. 如何在不关闭终端的情况下运行脚本?

scripts command-line gnome-terminal bash tabs
  • 2 2 个回答
  • 10012 Views

2 个回答

  • Voted
  1. Best Answer
    Gabriel Staples
    2020-02-12T15:23:39+08:002020-02-12T15:23:39+08:00

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

    我发现man bash并且man test也很有用。这是我想做的事情的方法,即:

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

    1st,将其添加到~/.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
    

    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;"
    

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

    ./open_tabs.sh
    

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

    在此处输入图像描述

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

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

    • 2
  2. Gewure
    2020-02-12T10:38:19+08:002020-02-12T10:38:19+08:00

    我认为你应该使用&让你的进程在后台运行。

    https://www.tecmint.com/run-linux-command-process-in-background-detach-process/

    • -1

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve