2012-10-11 39 views
3

我嘗試自動完成我的環境設置。這樣做我需要在終端中打開幾個選項卡並執行命令。標籤應該有一個標題來區分它們。該腳本只應寫入標籤中的命令而不執行。外殼腳本:爲新的GNOME終端標籤設置標題

該腳本需要一個輸入文件start.txt。這一行將逐行處理 - 每一行首先包含終端選項卡的標題,並用逗號分隔命令。

具有非常容易侏儒終端呼叫的標題將被顯示:

侏儒末端 - 標籤--title = TEST1 -e頂部--tab --title = TEST2頂部

但是,當一個複雜的命令,這將無法正常工作,標題不會被設置。

這裏是整個代碼:

#!/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)}\[email protected]\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)}\[email protected]\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; 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 \「」

當您只需複製此命令並在終端中執行它時,選項卡將顯示導出命令,但未包含在單引號中,則不起作用。

出口 PS1 = \ E] 0;使命\一] $ {debian_chroot:+($ debian_chroot)} \ü@ \ H:\ W $

我肯定更喜歡獲取gnome-terminal命令的標題選項,但如果這不可行,我會很高興提示如何在單引號中的標籤中輸出PS1的值。我已經試圖用\或幾個\逃脫它,但沒有成功。

回答

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