Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening terminal with multiple tabs using shell script

Tags:

linux

shell

I am new to linux shell script. I want to write a shell script which will open terminal with multiple tabs. And in each tab it should run one rtsp client app.

i have written this code,

tab="--tab-with-profile=Default -e "
cmd="java RunRTSPClient"
for i in 1 2 3 4 5
   do
#   
   foo="$foo $tab $cmd"         
   done
gnome-terminal $foo
exit 0

Its executing fine but it will open terminal and immediately it is closing.(I am not getting errors) If I replaced the line foo=... with gnome-terminal --tab -e $cmd then its working fine but opens independent terminal.

suggest me how to fix this.

thank you

like image 396
user1241903 Avatar asked Feb 12 '26 09:02

user1241903


1 Answers

You should always (always!) use an array for building up argument lists in bash.

That is:

#!/bin/bash
#    ^^ this has to be bash, not /bin/sh, for arrays to work
cmd=( gnome-terminal )
for ((i=0; i<5; i++)); do
  cmd+=( --tab-with-profile=Default -e "java RunRTSPClient" )
done
"${cmd[@]}"

This will give you the exact equivalent of running:

gnome-terminal \
  --tab-with-profile=Default -e "java RunRTSPClient" \
  --tab-with-profile=Default -e "java RunRTSPClient" \
  --tab-with-profile=Default -e "java RunRTSPClient" \
  --tab-with-profile=Default -e "java RunRTSPClient" \
  --tab-with-profile=Default -e "java RunRTSPClient"

...which is what I understand that you want.

Trying to build a complex command in a string causes Very Bad Things to happen; read http://mywiki.wooledge.org/BashFAQ/050 to understand why.

like image 121
Charles Duffy Avatar answered Feb 15 '26 04:02

Charles Duffy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!