Bash Counting Loop Syntax

Bash Counting Loop Syntax

Post by Martin G. McCormi » Fri, 27 Jul 2001 11:44:52



        I have been reading, rea-reading and re-re-reading the
man page on bash.  I seem to be stuck on the proper syntax for
making a simple counting loop.  The documentation says that one
can inclose a while statement in brases which makes since, but I
can't for the life of me produce an arithmetic expression that
the shell likes.  The man page even uses an example of
$ (( expression))

and I haven't hit the magic syntax to get anything other than
syntax errors.  The little script I tried is as follows:

#! /bin/sh
index=30
#Start index at 30.
while  { $(($index < 10));}  do
index=$(($index-1))
#this works by itself and produces 29 on the next line.
echo $index
done
exit 0

        The manual says that C-style expressions work, but the
errors I get with just this short loop shows that I no speak
language of shell to well yet.
--

Martin McCormick WB5AGZ  Stillwater, OK
OSU Center for Computing and Information Services Data Communications Group

 
 
 

Bash Counting Loop Syntax

Post by Allen Wo » Fri, 27 Jul 2001 23:48:49



Quote:>    I have been reading, rea-reading and re-re-reading the
> man page on bash.  I seem to be stuck on the proper syntax for
> making a simple counting loop.  The documentation says that one
> can inclose a while statement in brases which makes since, but I
> can't for the life of me produce an arithmetic expression that
> the shell likes.  The man page even uses an example of
> $ (( expression))
> and I haven't hit the magic syntax to get anything other than
> syntax errors.  The little script I tried is as follows:

<snip>

Try this:

Zaphod:~$ cat loop.sh
#!/bin/sh

index=30
while [ $index -lt 10 ]; do
    index=$(($index - 1 ))
    echo $index
done
exit 0

However, the logic of the script is flawed.  You set index to 30, then test if
index is *less* than 10.  Anyways, the script appears to work.  HTH

Allen

 
 
 

Bash Counting Loop Syntax

Post by Chri » Sat, 28 Jul 2001 00:16:20


Not being a programmer, I can't help with any C comparisons regarding
syntax; but here are a couple of things.

Of course, there's always more than one way to skin a cat.  I modded
your script into the following:

#!/usr/local/bin/bash
index=30
#Start index at 30.
while  [ $index -ge 10 ];  do
let index=$index-1
#this works by itself and produces 29 on the next line.
echo $index
done
exit 0

The differences include:

The Bang line fully qualifies the bash shell's path, /bin/sh is not
actually bash.
Different test syntax [] instead of () stuff
The script above prints numbers if they are greater than 10, not less.
 If you start at 30, and then tell it to subtract 1 while it's under
10, you won't get anything.  The first time it tests, it "should" say
"I'm not under ten, so I'm not going to do anything."

Regards,
Chris


Quote:> I have been reading, rea-reading and re-re-reading the
> man page on bash.  I seem to be stuck on the proper syntax for
> making a simple counting loop.  The documentation says that one
> can inclose a while statement in brases which makes since, but I
> can't for the life of me produce an arithmetic expression that
> the shell likes.  The man page even uses an example of
> $ (( expression))

> and I haven't hit the magic syntax to get anything other than
> syntax errors.  The little script I tried is as follows:

> #! /bin/sh
> index=30
> #Start index at 30.
> while  { $(($index < 10));}  do
> index=$(($index-1))
> #this works by itself and produces 29 on the next line.
> echo $index
> done
> exit 0

>    The manual says that C-style expressions work, but the
> errors I get with just this short loop shows that I no speak
> language of shell to well yet.

 
 
 

Bash Counting Loop Syntax

Post by Martin G. McCormi » Sat, 28 Jul 2001 00:59:40




>Not being a programmer, I can't help with any C comparisons regarding
>syntax; but here are a couple of things.

>Of course, there's always more than one way to skin a cat.  I modded
>your script into the following:

>#!/usr/local/bin/bash
>index=30
>#Start index at 30.
>while  [ $index -ge 10 ];  do
>let index=$index-1
>#this works by itself and produces 29 on the next line.
>echo $index
>done
>exit 0

        Many thanks.  In a case like this, the documentation is
complete, but just abstract enough that it is difficult to
translate some of it in to practical working shell script.

        Many years ago, somebody gave me a Bourn Shell script
that was an early attempt at autoconfiguration for various
packages that one might have installed on  a UNIX system in the
late eighties and early nineties.  One could look in that script
and see just about one of everything that one could put in a
Bourn Shell script.  Is there any mother load sort of script that
uses the Bourn Again Shell?  I have found that working examples
of syntax are the best way to get the point across.  It doesn't
matter what the script does or even if it ever was supposed to do
anything as long as the examples are syntactically correct?

        The bind distributions include a non-working
configuration file that is put together along these lines.  One
can look at it and beat on the examples until they look like what
we want right now.:-)
--

Martin McCormick WB5AGZ  Stillwater, OK
OSU Center for Computing and Information Services Data Communications Group

 
 
 

1. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

Is there a proper fixed bash on any of the FTP sites out there?

I know there bash is on the usual sites but I don't know if they are
bugged or not :(

Regards,

Neil.

--


------------------------------------| Edinburgh, EH14 2DE, United Kingdom
**Domino: There`s nothing you can do when you`re the next in line: Domino**

2. I can't find X11.msg.fr_FR.Dt.rte on CD in AIX 4.3.2

3. What is the bash syntax for [[ ]] - and how to find in "man bash" ?

4. setuid

5. Help with "alternate for-loop" syntax

6. icmp messages

7. Fix module ref counting in block/loop.c

8. Trying to get PC serial to unix modem to internet provider

9. Syntax error in nested loop?

10. count files from multiple directories within a loop (?)

11. looping...count="exact text"

12. Counting in a loop

13. How can I count in a FOR loop?