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

글로 표현해 보세요 \. 그러면 쉘은 옵션을 쓸 수 있는 다음 새 줄을 무시합니다.

답변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하는 데 파일 이름을 변수에 넣는 것을 고려할 수도 있습니다 .$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

관련 정보