Bash junk

Barry barry at hartford.uconn.edu
Wed Jan 17 13:28:25 PST 2001


Andy Goth wrote:
> 
> On Sunday, January 14, 2001 21:24, Simon Perreault wrote:
> > Oops, it should end with a 1, not a 0. I modified the script and
> > forgot to modify the output. Read like this:
> >
> > hi
> > 0
> > 1
> > 0
> > 0
> > 1
> 
> Mmm, I was looking at that output and scratching my balding seventeen-year-old head.  Thanks for clarifying.
> 
> [andy at chainsaw|~]$ false; echo m00; for i in 4; do echo ${?}; done
> m00
> 0
> [andy at chainsaw|~]$ false; for i in 4; do echo ${?}; done
> 1
> 
> Yeah, that confirms it.
> 
> 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]".
> 

your above example is rather odd... I'm not sure that it's indiscernable
from doing:

echo m00; echo $?

then:

false; echo $?

the first output isn't a surprise.  Basically it just echo'ed something
and then gave the return from the echo command.  false sets $? to 1,
essentially... so echo'ing $? afterwards SHOULD produce a 1...

the 'false' in the first command is useless because of the echo.  It
only printed once because it sees '4' as a word.  

for i in 4

tells bash to set i to 4 and then do whatever's between  do and done...

I don't use i because i is already set to something in my shell.  Make
sure that the shell variable that you're using is null before you wipe
out it's value :)

in otherwords:

# for x in 4; do echo $i; done
4
# for x in 4 5; do echo $i; done
4
5
# for x in hello goodbye; do echo $i; done
hello
goodbye


or, to show a functional usage:

# cd /usr/src/XF86/4.0.2; for filename in *.tgz; do tar zxvf $filename;
done
<tar output file1>
<tar output file2>
<tar output file3>


if you wanted to increment something, I usually use:

from 1 to 10

x=1;
while [ $x -lt 11 ]; 
do
	echo $x;
	x=$((x+1));
done;

alternately:

x=1;
until [ $x -gt 10 ]; 
do
	echo $x;
	x=$((x+1));
done;

I believe others also mentioned seq ... 

any of these will work for incrementation... for will only work like
that if you list the incremented numbers, i.e.:

for x in 1 2 3 4 5 6 7 8 9 10;

This *IS* useful in certain cases, but for just standard incrementation
it's kind of innefficient...

-- 
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