Skip to main content
edited tags
Link
Gilles 'SO- stop being evil'
  • 866.1k
  • 205
  • 1.8k
  • 2.3k

I am pretty sure this has been asked already in some form, I just cannot come up with a good find to search it.

I want to have a script that do things N times and to which I can pass as a variable what command to issue N times. In general the command might use the iterator value to change something. For instance I would like to do something like

~> doNtimes.sh 10 0 "ls *$(($2 + $i ))*.gnu | wc -l;"

where doNtime.sh is something like

  for ((i=0; i < $1; ++i)); do 
       echo "iterator=$i"; 
       $3
  done

of course the current use of double quotes make me fail. I have tried with single quotes but does not work either (although for a different reason). The fact that I have variable with spaces and contains variable to be evaluated in the script makes impossible for me to find the right syntax ... any idea?

Thanks a lot. Cheers, Roberto

I am pretty sure this has been asked already in some form, I just cannot come up with a good find to search it.

I want to have a script that do things N times and to which I can pass as a variable what command to issue N times. In general the command might use the iterator value to change something. For instance I would like to do something like

~> doNtimes.sh 10 0 "ls *$(($2 + $i ))*.gnu | wc -l;"

where doNtime.sh is something like

  for ((i=0; i < $1; ++i)); do 
       echo "iterator=$i"; 
       $3
  done

of course the current use of double quotes make me fail. I have tried with single quotes but does not work either (although for a different reason). The fact that I have variable with spaces and contains variable to be evaluated in the script makes impossible for me to find the right syntax ... any idea?

Thanks a lot. Cheers, Roberto

I am pretty sure this has been asked already in some form, I just cannot come up with a good find to search it.

I want to have a script that do things N times and to which I can pass as a variable what command to issue N times. In general the command might use the iterator value to change something. For instance I would like to do something like

~> doNtimes.sh 10 0 "ls *$(($2 + $i ))*.gnu | wc -l;"

where doNtime.sh is something like

  for ((i=0; i < $1; ++i)); do 
       echo "iterator=$i"; 
       $3
  done

of course the current use of double quotes make me fail. I have tried with single quotes but does not work either (although for a different reason). The fact that I have variable with spaces and contains variable to be evaluated in the script makes impossible for me to find the right syntax ... any idea?

Source Link
Rho Phi
  • 319
  • 4
  • 12

variable substitution in CLI and for loops

I am pretty sure this has been asked already in some form, I just cannot come up with a good find to search it.

I want to have a script that do things N times and to which I can pass as a variable what command to issue N times. In general the command might use the iterator value to change something. For instance I would like to do something like

~> doNtimes.sh 10 0 "ls *$(($2 + $i ))*.gnu | wc -l;"

where doNtime.sh is something like

  for ((i=0; i < $1; ++i)); do 
       echo "iterator=$i"; 
       $3
  done

of course the current use of double quotes make me fail. I have tried with single quotes but does not work either (although for a different reason). The fact that I have variable with spaces and contains variable to be evaluated in the script makes impossible for me to find the right syntax ... any idea?

Thanks a lot. Cheers, Roberto