I have a script that open up terminal and open up 5 tabs, execute a certain command, and go to a certain working directory.
#!/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"
Having everything in one line is hard to maintain. How do I make it better so it is easy to read ?
I've tried
#!/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
So far it is not working yet! I'm open to any suggestions that you guys may have for me. I'm just looking to learn and improve it.
Tab=()
and when adding elements, don't quote the whole thing:Tab+=(--tab --title='SSC-DB' --profile Hold --working-directory='/home/benu/SSC-V2/ssc-db')
– glenn jackman Feb 13 '16 at 16:28Tab
you are basically calling likegnome-terminal "" "--tab --title blah" "--tab --title blahblah"
which is definitely not what you want. Just quote as how you would quote it normally, like in @glennjackman's suggestion. Note that you don't need to explicitly do any array initialization unless you are working withset -u
. (Your current one adds an empty string element though). – Mingye Wang Feb 13 '16 at 20:07bash
stuff, so use#!/bin/bash
as your shebang. – Mingye Wang Feb 13 '16 at 20:07