Brian Jackson on wrote...
| On Monday 02 June 2008 09:29, Fred Weinhaus wrote:
| > Not correct while syntax for unix
| >
| > replace for i while i<300
| >
| > with (note space after [ and before ])
| >
| > while [ $i -lt 300 ]
| > do
| > ...
| > done
|
| You can also do this:
|
| for i in {1..300}; do
| echo $i
| done
|
| No need to increment the counter :)
|
Reports are that this does not work with the bash on MacOSX
It just loops once with i='{1..300}'
But it can use...
for (( i=0; i < 300; i++ ))
MacOXS unfortunatally also does not install the GNU 'seq' command
either. The 'seq' can generate a list of numbers forward, backward,
different increments and even floating point and format output control!
For example...
seq 10
seq 5 10
seq 10 -1 1
seq 1 .1 2
seq -f %03g 5 5 100
As such to make a script more 'universal' but the loop easier to read
I would include a 'prefix' such as..
=======8<--------CUT HERE----------axes/crowbars permitted---------------
cmd_found() {
case "`type $1 2>&1`" in *'not found'*) return 1 ;; esac; return 0
}
if cmd_found seq; then
: do nothing
else
seq() {
n=$1 while [ $n -le $2 ]; do echo $n; n=`expr $n + 1`; done
}
}
=======8<--------CUT HERE----------axes/crowbars permitted---------------
and can then use a loop like...
=======8<--------
for i in `seq 0 300`; do
echo $i
done
=======8<--------
Looks a lot neater than adding the 'while' for every counted loop.
This is also valid 'Bourne Shell' not just BASH (Born Again SHell),
so will work on very old UNIX's too.
Other methods of generating a lists (or files) of numbers include..
Perl
perl -le 'for(1..10){print}'
Cat
Specifically dd | tr | cat
This reads 10 (or however many) 'nulls' from /dev/zero, then
converts that into 10 return characters, which cat numbers.
dd 2>/dev/null if=/dev/zero bs=10 count=1 | tr \\0 \\012 |\
cat -n | tr -d ' \11'
This last works on all UNIX's too, though is a bit grungy.
The above comes from various shell hints and tips
http://www.cit.gu.edu.au/~anthony/info/shell/file.hints
and http://www.cit.gu.edu.au/~anthony/info/shell/script.hints
You may also find... Bash - Why NOT! interesting
http://www.cit.gu.edu.au/~anthony/info/shell/bash.whynot
Anthony Thyssen ( System Programmer ) <[EMAIL PROTECTED]>
-----------------------------------------------------------------------------
Dr Basher: "Who told you that?" Miles O'Brian: "You did!"
"Well who am I to argue with me!!" -- StarTrek DS9, "Visions"
-----------------------------------------------------------------------------
Anthony's Home is his Castle http://www.cit.gu.edu.au/~anthony/
_______________________________________________
Magick-users mailing list
[email protected]
http://studio.imagemagick.org/mailman/listinfo/magick-users