Operating System - HP-UX
1751968 Members
4827 Online
108783 Solutions
New Discussion юеВ

Re: Another Newbie Question 'For Syntax'

 
SOLVED
Go to solution
Scott Frye_1
Super Advisor

Another Newbie Question 'For Syntax'

If I were writing this in C I would write:
For($i=0; $i<$total; $i+9) {
whatever I want to do
}

Does Posix have a similar syntax to do this? I've looked in the sh-posix man pages and haven't come up with this type of loop. Can anyone help me again?

Thanks

Scott
5 REPLIES 5
Sundar_7
Honored Contributor
Solution

Re: Another Newbie Question 'For Syntax'

You will have to try something like this with while

typeset -i COUNT=0
MAX=10

while [[ $COUNT -lt $MAX ]]
do

(( COUNT=COUNT+1 ))
done
Learn What to do ,How to do and more importantly When to do ?
Scott Frye_1
Super Advisor

Re: Another Newbie Question 'For Syntax'

Wasn't what I was looking for but it worked like a charm. Thank you for your input.
Muthukumar_5
Honored Contributor

Re: Another Newbie Question 'For Syntax'

You can use until format for this too as,

i=0
total=10

until [[ $i -gt $total ]]
do

echo $i


# loop increment
let i=i+1
done

Easy to suggest when don't know about the problem!
Sundar_7
Honored Contributor

Re: Another Newbie Question 'For Syntax'

Scott,

This may not be what you are looking for. But as far as I know, there is no loop construct like that in Posix.

FOR loop in posix will only accept list of values, not the range of values.

You can try something like this

for I in 0 1 2 3 4 5 6 7 8 9

But clearly, it is not an elegant or effective way of scripting.

-- Sundar.
Learn What to do ,How to do and more importantly When to do ?
Muthukumar_5
Honored Contributor

Re: Another Newbie Question 'For Syntax'

We can use awk programming to use exact format of c as,

i=0
total=10

echo "$i $total" | awk '{ for ( i=$1; i<=$2; i++) }'

- Muthu
Easy to suggest when don't know about the problem!