Shell脚本:为新的gnome-terminal选项卡设置标题

时间:2022-06-11 06:55:25

I try to automate the setup of my environment. Doing this I need to open several tabs in a terminal and execute commands. The tabs should have a title to distinguish them. The script should only write the commands in the tabs without executing.

我尝试自动设置我的环境。为此,我需要在终端中打开几个选项卡并执行命令。标签应该有一个标题来区分它们。脚本应该只在选项卡中编写命令而不执行。

The script expects an input file start.txt. This one will be processed line by line - each line contains first the title for the terminal tab and separated with a comma the command.

脚本要求输入文件start.txt。这一行将逐行处理——每一行首先包含终端选项卡的标题,并用逗号分隔命令。

With a very easy gnome-terminal call the title will be shown:

使用一个非常简单的gnome-terminal调用,标题将显示:

gnome-terminal -- tab --title=test1 -e top --tab --title=test2 top

gnome-terminal——tab -title=test1 -e top -tab -title=test2 top

But when taking a complex command this won't work and the title will not be set.

但是当接受一个复杂的命令时,这将不起作用,标题也不会被设置。

Here is the whole code:

这是整个代码:

#!/bin/bash

# define variable which needs to be executed
cmdline="gnome-terminal"
# define input file
file="cat start.txt"
# define run_command script
destdir=/home/ank1abt/Documents/run_command.sh


# create if not already existing and change permissions
touch $destdir
chmod 755 $destdir

# read file an process line by line 
# each line contains first the title and with comma separated the command for a new tab in a terminal
$file | \
while read row; do
  #extract tab title and command   
  title=$(echo $row | awk -F","  '{print $1}')
  cmd=$(echo $row | awk -F","  '{print $2}')  
  set_title="export PS1='\[\e]0;"$title"\a\]\${debian_chroot:+(\$debian_chroot)}\u@\h:\w\$'"      
  #cmdline=$cmdline\ "--tab --title="$title" -e top"
  cmdline=$cmdline\ "--tab --title="$title" -e \"bash -c \\\" echo "$title"; echo export PS1='\[\e]0;"$title"\a\]\\\${debian_chroot:+(\\\$debian_chroot)}\u@\h:\w\$';echo "$cmd"; exec bash\\\"\""
  echo $cmdline   
  # command will be written to a file
  echo "$cmdline" > $destdir
done

# execute the file with the command
exec "./run_command.sh"

In the meantime I tried a workaround. There is another interesting command with which you could set the tab title from within the tab which could then be executed in the tab or just written there so that the user can copy and execute it:

与此同时,我尝试了一个变通办法。还有一个有趣的命令,你可以在选项卡中设置选项卡标题,然后在选项卡中执行,或者直接写在那里,这样用户就可以复制并执行:

export PS1='[\e]0;mission\a]${debian_chroot:+($debian_chroot)}\u@\h:\w$'

出口PS1 = '[\ e]0;任务\]$ { debian_chroot:+(debian_chroot美元)} \ u@ \ h:\ w美元”

But with the current code the following command will be written to the run_command script:

但是使用当前代码,下面的命令将被写入run_command脚本:

gnome-terminal --tab --title=test1 -e "bash -c \" echo test1; echo export PS1='[\e]0;test1\a]\${debian_chroot:+(\$debian_chroot)}\u@\h:\w$'; echo top; exec bash\"" --tab --title=test2 -e "bash -c \" echo test2; echo export PS1='[\e]0;test2\a]\${debian_chroot:+(\$debian_chroot)}\u@\h:\w$'; echo top; exec bash\""

gnome-terminal——tab——title=test1 -e“bash -c \”echo test1;回声出口PS1 = '[\ e]0;test1 \]\ $ { debian_chroot:+(\ debian_chroot美元)} \ u@ \ h:\ w美元”;回波顶;exec bash\“”——tab——title=test2 -e“bash -c \”echo test2;回声出口PS1 = '[\ e]0;test2 \]\ $ { debian_chroot:+(\ debian_chroot美元)} \ u@ \ h:\ w美元”;回波顶;exec bash \”“

When you just copy this command and execute it in a terminal the tabs will show the export command but not enclosed in single quotes and then it won't work.

当您复制此命令并在终端中执行时,选项卡将显示导出命令,但不包含在单引号中,那么它将无法工作。

export PS1=[\e]0;mission\a]${debian_chroot:+($debian_chroot)}\u@\h:\w$

出口PS1 =[\ e]0;任务\]$ { debian_chroot:+(debian_chroot美元)} \ u@ \ h:\ w美元

For sure I prefer to get the title option of the gnome-terminal command working but if this won't be possible I would be happy about any hints how to have the values for the export of PS1 in the tabs in singe quotes. I already tried to escape it with \ or several\ without success.

当然,我更喜欢使用gnome-terminal命令的标题选项,但如果这是不可能的,那么我很乐意看到任何提示如何在singe引用的选项卡中输出PS1的值。我已经试着用\或几个\来逃避,但没有成功。

1 个解决方案

#1


1  

Since you are writing the command to a file and then executing it, you need an extra level of quoting. Try this in the middle part of the script:

因为您正在将该命令写入文件,然后执行它,所以需要额外的引用级别。在剧本的中间部分试试这个:

while read row; do
  #extract tab title and command   
  title=$(echo $row | awk -F","  '{print $1}')
  cmd=$(echo $row | awk -F","  '{print $2}')  
  cmdline=$cmdline\ "--tab --title='$title' -e '$cmd'"
  echo $cmdline   
  # command will be written to a file
  echo "$cmdline" > $destdir
done

#1


1  

Since you are writing the command to a file and then executing it, you need an extra level of quoting. Try this in the middle part of the script:

因为您正在将该命令写入文件,然后执行它,所以需要额外的引用级别。在剧本的中间部分试试这个:

while read row; do
  #extract tab title and command   
  title=$(echo $row | awk -F","  '{print $1}')
  cmd=$(echo $row | awk -F","  '{print $2}')  
  cmdline=$cmdline\ "--tab --title='$title' -e '$cmd'"
  echo $cmdline   
  # command will be written to a file
  echo "$cmdline" > $destdir
done