Search code examples
bashshellloopsexecutiongnome-terminal

SCRIPT - Multiple programs in multiple terminals


I made a script so I can easily execute multiple programs in different terminals by just writing

./script.sh (numberofprogramstype1) (numberofprogramstype2)

The problem is it only executes one of each type, even though I am looping it based on the arguments given. Here is my code:

gcc program1.c -lpthread

for i in {1..$1}
do
       gnome-terminal -e ./a.out
done

gcc program2.c -lpthread

for i in {1..$2}
do
        gnome-terminal -e ./a.out
done

Why is this happening and how can I solve it?


Solution

  • Brace expansion happens before parameter expansion; you can only use hard-coded numbers in them. Use a C-style for loop instead.

    # {1..$1}
    for ((i=1;i<=$1;i++)); do
       ...