環境のセットアップを自動化しようとしています。これを行うには、ターミナルでいくつかのタブを開いてコマンドを実行する必要があります。タブには、それらを区別するためのタイトルが必要です。スクリプトは、実行せずにタブにコマンドを書き込むだけにする必要があります。
スクリプトは、入力ファイル 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"
その間、私は回避策を試しました。タブ内からタブのタイトルを設定できる別の興味深いコマンドがあり、それをタブで実行するか、ユーザーがコピーして実行できるようにそこに書き込むことができます。
export PS1='[\e]0;mission\a]${debian_chroot:+($debian_chroot)}\u@\h:\w$'
しかし、現在のコードでは、次のコマンドが 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$'; エコートップ; exec bash\"" --tab --title=test2 -e "bash -c \" echo test2; echo export PS1='[\e]0;test2\a]\${debian_chroot:+(\$debian_chroot)}\u@\h:\w$'; エコートップ; exec bash\""
このコマンドをコピーしてターミナルで実行すると、タブに export コマンドが表示されますが、一重引用符で囲まれていないため、機能しません。
export PS1=[\e]0;mission\a]${debian_chroot:+($debian_chroot)}\u@\h:\w$
確かに、gnome-terminal コマンドのタイトル オプションを機能させたいと思っていますが、これが不可能な場合は、タブで PS1 のエクスポートの値を一重引用符で囲む方法のヒントをいただければ幸いです。私はすでに\またはいくつか\でエスケープしようとしましたが、成功しませんでした。