我尝试自动设置环境。这样做我需要在终端中打开几个选项卡并执行命令。选项卡应具有标题以区分它们。该脚本应该只在选项卡中编写命令而不执行。
脚本需要一个输入文件start.txt。这一行将逐行处理 - 每行首先包含终端选项卡的标题,并用逗号分隔命令。
通过非常简单的gnome-terminal呼叫,将显示标题:
gnome-terminal - tab --title = test1 -e top --tab --title = test2 top
但是当采用复杂的命令时,这将不起作用,并且不会设置标题。
以下是整个代码:
#!/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"
与此同时,我尝试了一种解决方法。还有另一个有趣的命令,您可以在选项卡中设置选项卡标题,然后可以在选项卡中执行或只是在那里写入,以便用户可以复制并执行它:
出口 PS1 = '[\ E] 0;任务\ A] $ {debian_chroot:+($ debian_chroot)} \Ú@ \ H:\ W $'
但是使用当前代码,以下命令将写入run_command脚本:
gnome-terminal --tab --title = test1 -e“bash -c \”echo test1;回声 出口 PS1 = '[\ E] 0; TEST1 \一个] \ $ {debian_chroot:+(\ $ debian_chroot)} \Ú@ \ H:\ W $'; 回声顶部; exec bash \“” - tab --title = test2 -e“bash -c \”echo test2; 回声输出 PS1 = '[\ E] 0; TEST2 \一个] \ $ {debian_chroot:+(\ $ debian_chroot)} \Ú@ \ H:\ W $'; 回声顶部; exec bash \“”
当您只是复制此命令并在终端中执行它时,选项卡将显示导出命令但不包含在单引号中,然后它将不起作用。
出口 PS1 = [\ E] 0;任务\ A] $ {debian_chroot:+($ debian_chroot)} \Ú@ \ H:\ W $
我肯定希望得到gnome-terminal命令的title选项,但是如果这不可能,我会很高兴任何提示如何在单引号中的选项卡中输出PS1的值。我已经试图用\或几个\来逃避它而没有成功。
答案 0 :(得分:1)
由于您正在将命令写入文件然后执行它,因此您需要额外的引用级别。在脚本的中间部分尝试这个:
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