awrit
awrit

Reputation: 11

How to implement a "for" loop under an "if" condition in shell

I have a requirement where in, I need to use a certain set of values, under a Nested if condition. I am trying to implement it using a for loop, but not able to put a correct syntax.

eg.

if [ "$i" -ge 353 ] && [ "$i" -lt 385 ]
then
   for((m=7001;m<7016;m++))
value=$m
done
fi

Is this a correct syntax?

Upvotes: 0

Views: 118

Answers (1)

fedorqui
fedorqui

Reputation: 289565

You need an extra do for the for loop. Formatting it makes it more clear:

if [ "$i" -ge 353 ] && [ "$i" -lt 385 ]
then
   for((m=7001;m<7016;m++))
   do
      value=$m
   done
fi

Note that all these while, for, etc need to be wrapped with do and done:

while #something
do
    things
done

for #condition
do
    things
done

'done' indicates that the code that used the value of $i has finished and $i can take a new value.

And for the if:

if #condition; then
    things
fi

Upvotes: 5

Related Questions