Bash junk

Simon Perreault nomis80 at videotron.ca
Sun Jan 14 19:50:38 PST 2001


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sunday 14 January 2001 22:35, Andy Goth wrote:
> Speaking of for, how can I go through a range of numbers?  Is it possible??
>  Y'know, like "for (int i = 0; i < 10; i++)" for i to go from 0 - 9.  Up
> until now I've been forced to do some weird while stuff incrementing with
> the clumsy "i=$[${i} + 1]".

You can do fors like this:

for (( expr1 ; expr2 ; expr3 ))
do
	commands
done

This is exactly the same as in C. expr1 is evaluated when the for is started. 
Then expr2 is evaluated and if the retval of expr2 is 0 (not 1 as in C 
though) then the for loop is executed. At the end of the loop, expr3 is 
evaluated, and then the test condition expr2 is evaluated again to see if the 
loop should be re-entered. expr3 is usually the increment expression.

Also, you can go though sequences of numbers using the seq command. Seq uses 
this syntax:
seq <last>
  OR
seq <first> <last>
  OR
seq <first> <increment> <last>

For example, to do a loop five times with i as the variable holding the loop 
number, do:

for i in $( seq 5 )
do
	echo $i
done

Do seq --help for more info.

- -- 
Simon Perreault, 17 year old college student, UNIX newbie
Public key: http://nomis80.dyn.dhs.org/nomis80.gpg
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE6YnOOipu7RIft+3sRArTZAJ0e5PpTnNS1KzVWWZPVyip5pBj9HACg2Xvc
9TXN1pCCWPfFmlp2GV5nBQo=
=n3g/
-----END PGP SIGNATURE-----

-- 
Unsubscribe: send email to lfs-discuss-request at linuxfromscratch.org
and put unsubscribe in the subject header of the message




More information about the lfs-dev mailing list