0

can any of you tell me what is shell's problem in this case?

When I write and execute this script(./cyklus.sh 5 10):

  1 #!/bin/bash
  2 
  3 
  4 if [ $# -eq 2 ]; then
  5         if [ $1 -le $2 ]; then
  6                 for i in {$1..$2}
  7                 do
  8                         if [ $i -lt $2 ]; then
  9                                 echo -n "$i "
 10                         else 
 11                                 echo "$i"
 12                         fi
 13                 done            
 14         elif [ $1 -gt $2 ]; then
 15                 echo "Prvy argument musi byt vacsi nez druhy."
 16         fi
 17 else            
 18         echo "Nezadal si 2 argumenty"
 19 fi

it outputs an error:

./cyklus.sh: line 8: [: {5..10}: integer expression expected
{5..10}

even though the starting and ending integer is set there through $1 and $2.

alik33
  • 141
  • 3
  • 13
  • Expansion order says you can't do that. Brace expansion happens first and doesn't see `{$1..$2}` as valid. – Etan Reisner Nov 04 '15 at 13:58
  • so can you tell me how to fix that? – alik33 Nov 04 '15 at 13:59
  • Run it in zsh instead. –  Nov 04 '15 at 14:00
  • I would like to run it in zsh but the problem is that I have an exam and their tester doesnt accept that – alik33 Nov 04 '15 at 14:01
  • I know I can use C's syntax for this loop and it will be working but I just wondered why it doesnt work this way. Sequences expansion and variable in bash which was posted by Etan Reisner helped me, thank you. – alik33 Nov 04 '15 at 14:05

1 Answers1

1

You can use "seq" command to generate the sequence:

for i in `seq $1 $2`
SimoneLazzaris
  • 329
  • 2
  • 9