如何將 bash 腳本從一長行改進為更好的方法?

如何將 bash 腳本從一長行改進為更好的方法?

我有一個腳本,可以打開終端機並打開 5 個選項卡,執行某個命令,然後轉到某個工作目錄。

#!/bin/sh

gnome-terminal --tab --title="Zookeeper" --profile Hold -e "sh -c '/home/kafka_2.11-0.8.2.2/bin/zookeeper-server-start.sh /home/kafka_2.11-0.8.2.2/config/zookeeper.properties'" --tab --title="Kafka" --profile Hold -e "sh -c 'sleep 5; /home/kafka_2.11-0.8.2.2/bin/kafka-server-start.sh /home/kafka_2.11-0.8.2.2/config/server.properties'" --tab --title="APP-Binaries" --profile Hold --working-directory="/home/app-binaries" --tab --title="APP-DB" --profile Hold --working-directory="/home/prod/db"

將所有內容都排在一行中很難維護。我怎麼能讓它變得更好,以便於閱讀?

我試過了

#!/bin/sh

Tab=""

Tab+=("--tab --title='Zookeeper' --profile Hold -e 'sh -c /home/kafka_2.11-0.8.2.2/bin/zookeeper-server-start.sh /home/kafka_2.11-0.8.2.2/config/zookeeper.properties'")
Tab+=( "--tab --title='Kafka' --profile Hold -e 'sh -c 'sleep 5; /home/kafka_2.11-0.8.2.2/bin/kafka-server-start.sh /home/kafka_2.11-0.8.2.2/config/server.properties'")
Tab+=(" --tab --title='APP-Binaries' --profile Hold --working-directory='/home/app-binaries'")
Tab+=(" --tab --title='APP-DB' --profile Hold --working-directory='/home/prod/db'")
    
# echo "${Tab[@]}"
    
gnome-terminal "${Tab[@]}"
    
exit 0

目前還沒有發揮作用!我願意接受你們對我提出的任何建議。我只是想學習並改進它。

答案1

您可以使用\將長命令拆分為多行。

例子:

#!/bin/bash

echo "Hello World!"
echo \
"Hello World!"

運行此腳本會導致

$ ./test.sh 

Hello World!
Hello World!

在你的情況下,你可以使用類似的東西

#!/bin/bash    

gnome-terminal \
--tab --title="Zookeeper" --profile Hold -e "sh -c '/home/benu/Downloads/kafka_2.11-0.8.2.2/bin/zookeeper-server-start.sh /home/benu/Downloads/kafka_2.11-0.8.2.2/config/zookeeper.properties'" \
--tab --title="Kafka" --profile Hold -e "sh -c 'sleep 5; /home/benu/Downloads/kafka_2.11-0.8.2.2/bin/kafka-server-start.sh /home/benu/Downloads/kafka_2.11-0.8.2.2/config/server.properties'" \
--tab --title="SSC" --profile Hold -e "sh -c 'sleep 15; cd ~/gitnewssc/benu-ssc-binaries; ./startSSC.sh'" --working-directory="/home/benu/gitnewssc/benu-ssc-binaries" \
--tab --title="SSC-Binaries" --profile Hold --working-directory="/home/benu/gitnewssc/benu-ssc-binaries" \
--tab --title="SSC-DB" --profile Hold --working-directory="/home/benu/SSC-V2/ssc-db"

答案2

嘗試用寫作來表達\。然後 shell 會忽略下一個新行,您可以在新行中寫入選項。

答案3

您可以使用陣列使用形式對您的原始想法進行少量編輯arrayName[number]="tab assignment" 。例如,這是我在互動式終端機會話中開啟新終端機視窗所做的操作(所有這些步驟都可以輕鬆變成腳本)。

$ array[0]=" --tab --title 'Tab1' -e vi"                       

$ array[1]=" --tab --title 'Tab1' -e byobu"                    

$ gnome-terminal ${array[@]}

Genn Jackman 在評論中正確地註意到引用可能會成為一個問題,特別是如果您有幾個像您的例子一樣困難的命令。因此,您可能需要考慮將選項卡的資訊和它需要運行的實際命令拆分為兩個相應的數組,每個選項卡都有一個數組條目。請注意,我們希望將其用作cmd[x]一個完整的字串,因此需要引用它,而tabinfo[1]必須擴展到幾個不同的選項

您可能還需要考慮將檔案名稱放入變數中,例如VAR=/path/to/file和 using$VAR來引用命令內部的檔案名,而不是使用冗長的命令。

#!/bin/bash

function main()
{
   local cmd[1]="sh -c 'df;free;bash'"
   local cmd[2]="sh -c 'lsblk;cat /etc/fstab;bash'"
   local tabinfo[1]="--tab 'TAB1' --profile CRT -e"
   local tabinfo[2]="--tab 'TAB2'"
   gnome-terminal  ${tabinfo[1]} "${cmd[1]}" ${tabinfo[2]} "${cmd[2]}"
}

main

相關內容