I don't have much knowledge of shell scripting. I have a command which is used multiple times but value is modifying.
Here is my script:
#!/bin/bash
tab=" --tab-with-profile=Default"
options=(--tab --title=Terminal)
cmd[1]="ping 192.168.9.10"
title[1]="10"
cmd[2]="ping 192.168.9.20"
title[2]="20"
cmd[3]="ping 192.168.9.30"
title[3]="30"
cmd[4]="ping 192.168.9.23"
title[4]="23"
cmd[5]="ping 192.168.9.26"
title[5]="26"
cmd[6]="ping 192.168.9.40"
title[6]="40"
cmd[7]="ping 192.168.9.41"
title[7]="41"
cmd[8]="ping 192.168.9.42"
title[8]="42"
cmd[9]="ping 192.168.0.43"
title[9]="43"
cmd[10]="ping 192.168.9.50"
title[10]="50"
cmd[11]="ping 192.168.9.11"
title[11]="11"
cmd[12]="ping 192.168.9.12"
title[12]="12"
for i in 1 2 3 4 5 6 7 8 9 10 11 12; do
options+=($tab --title="${title[i]}" -e "bash -c \"${cmd[i]} ; bash\"" )
done
gnome-terminal "${options[@]}"
exit 0
I don't want to write cmd[i]
and title[i]
for multiple times. I want to write one single command that execute it multiple times.I don't know how to use for
or while
loop and execute a single command for different process rather multiple times.
You could simplify your code a bit:
This will set the titles to be
9.10
,9.20
, etc. If you just want the part after.
, you could use--title=${i##*.}
instead of just--title=$i
.