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.
With a very easy gnome-terminal call the title will be shown:
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$'
But with the current code the following command will be written to the run_command script:
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\""
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$
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With