Skip to main content
added 155 characters in body
Source Link
Hauke Laging
  • 94.6k
  • 21
  • 132
  • 185

You need eval:

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

Of course, you must pay attention to the right quoting in the command string. Your example ls *$(($2 + $i ))*.gnu | wc -l isn'tis dangerous in this sense, though because the variable references are resolved within "" i.e. before the script runs. You need single quotes:

doNtimes.sh 10 0 'ls *$(($2 + $i ))*.gnu | wc -l'

You need eval:

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

Of course, you must pay attention to the right quoting in the command string. Your example ls *$(($2 + $i ))*.gnu | wc -l isn't dangerous in this sense, though.

You need eval:

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

Of course, you must pay attention to the right quoting in the command string. Your example ls *$(($2 + $i ))*.gnu | wc -l is dangerous in this sense because the variable references are resolved within "" i.e. before the script runs. You need single quotes:

doNtimes.sh 10 0 'ls *$(($2 + $i ))*.gnu | wc -l'
Source Link
Hauke Laging
  • 94.6k
  • 21
  • 132
  • 185

You need eval:

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

Of course, you must pay attention to the right quoting in the command string. Your example ls *$(($2 + $i ))*.gnu | wc -l isn't dangerous in this sense, though.