bash question

bash question

am 01.04.2004 02:45:30 von scott.smallsreed

This is a multi-part message in MIME format.

------=_NextPart_000_0128_01C4173F.93A180B0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

I can't get this bash script to work. It's suppose to print all odd numbers
from 1 to 10.

#!/usr/bin/bash

LIMIT=10
a=1

while [ "$a" -le $LIMIT ]
do
if [ "$a" -eq $(1 3 5 7 9) ] <---------Something not right here.
then
echo -n "$a "
fi

echo

let "a+=1"
done
echo; echo
exit 0


Can anyone figure out what I am doing wrong? Can I also see the same thing
written in Perl?

Thanks for your help.



Scott D. Smallsreed
3030 Chipmunk Dr.
Washoe Valley, NV 89704
775.849.8411 Hm
775.849.8412 Fax
775.722.7773 Cell

------=_NextPart_000_0128_01C4173F.93A180B0
Content-Type: text/x-vcard;
name="Scott Smallsreed.vcf"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="Scott Smallsreed.vcf"

BEGIN:VCARD
VERSION:2.1
N:Smallsreed;Scott
FN:Scott Smallsreed
TEL;HOME;VOICE:775-849-8411
TEL;HOME;FAX:775-849-8412
ADR;HOME:;;3030 Chipmunk Dr.;Washoe Valley;Nevada;89704;US
LABEL;HOME;ENCODING=3DQUOTED-PRINTABLE:3030 Chipmunk Dr.=3D0D=3D0AWashoe =
Valley, Nevada 89704=3D0D=3D0AUS
EMAIL;PREF;INTERNET:scott.smallsreed@mindspring.com
REV:20040401T004530Z
END:VCARD

------=_NextPart_000_0128_01C4173F.93A180B0--

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: bash question

am 01.04.2004 09:03:31 von Luca Ferrari

On Thursday 01 April 2004 02:45 Scott@Charter's cat walking on the keyboard
wrote:

> I can't get this bash script to work. It's suppose to print all odd
> numbers from 1 to 10.
>
> #!/usr/bin/bash
>
> LIMIT=10
> a=1
>
> while [ "$a" -le $LIMIT ]
> do
> if [ "$a" -eq $(1 3 5 7 9) ] <---------Something not right here.

This is an awkard way to print all odd numbers, have a look at this:

#!/bin/bash

LIMIT=10
a=1

while test ${a} -lt ${LIMIT}
do
go=`expr ${a} % 2`
if test ${go} -ne 0
then
echo "Odd number ${a}"
fi

a=`expr ${a} + 1`

done


exit 0


In this example, if you change the LIMIT value to 100, the script will print
you all odds numbers, while in your example you have to put numbers by your
own. In perl it can result as:

#!/usr/bin/perl

$LIMIT=10;
$a=1;

while( $a < $LIMIT ){
if( ($a % 2) !=0 ){
print "Odd number $a\n";
}
$a++;
}

exit;


Luca


--
Luca Ferrari,
fluca1978@virgilio.it
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: bash question

am 01.04.2004 16:58:05 von Jeff Largent

And just to show another way:

#!/bin/bash

for i in `seq 1 10`; do
if [ $((${i} % 2)) != 0 ]; then
echo ${i}
fi
done



Luca Ferrari wrote:
> On Thursday 01 April 2004 02:45 Scott@Charter's cat walking on the keyboard
> wrote:
>
>
>>I can't get this bash script to work. It's suppose to print all odd
>>numbers from 1 to 10.
>>
>>#!/usr/bin/bash
>>
>>LIMIT=10
>>a=1
>>
>>while [ "$a" -le $LIMIT ]
>>do
>> if [ "$a" -eq $(1 3 5 7 9) ] <---------Something not right here.
>
>
> This is an awkard way to print all odd numbers, have a look at this:
>
> #!/bin/bash
>
> LIMIT=10
> a=1
>
> while test ${a} -lt ${LIMIT}
> do
> go=`expr ${a} % 2`
> if test ${go} -ne 0
> then
> echo "Odd number ${a}"
> fi
>
> a=`expr ${a} + 1`
>
> done
>
>
> exit 0
>
>
> In this example, if you change the LIMIT value to 100, the script will print
> you all odds numbers, while in your example you have to put numbers by your
> own. In perl it can result as:
>
> #!/usr/bin/perl
>
> $LIMIT=10;
> $a=1;
>
> while( $a < $LIMIT ){
> if( ($a % 2) !=0 ){
> print "Odd number $a\n";
> }
> $a++;
> }
>
> exit;
>
>
> Luca
>
>

--
Jeff Largent ImageLinks, Inc.
Sr System Admin Melbourne, Fl 32935
(321) 253-0011 fax: (321) 253-5559

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: bash question

am 01.04.2004 21:46:53 von Chuck Harding

How about

#!/bin/bash
for ((i=1;i<11;i+=2))
do
echo $i
done


man bash shows the usage of the arithmetic for loop which I use all
the time to execute remote commands on systems whose hostnames vary
by an integer node number - e.g., host1, host2, host3, ... host128

On Thu, 1 Apr 2004, Jeff Largent wrote:

> And just to show another way:
>
> #!/bin/bash
>
> for i in `seq 1 10`; do
> if [ $((${i} % 2)) != 0 ]; then
> echo ${i}
> fi
> done
>
>
>
> Luca Ferrari wrote:
> > On Thursday 01 April 2004 02:45 Scott@Charter's cat walking on the keyboard
> > wrote:
> >
> >
> >>I can't get this bash script to work. It's suppose to print all odd
> >>numbers from 1 to 10.
> >>
> >>#!/usr/bin/bash
> >>
> >>LIMIT=10
> >>a=1
> >>
> >>while [ "$a" -le $LIMIT ]
> >>do
> >> if [ "$a" -eq $(1 3 5 7 9) ] <---------Something not right here.
> >
> >
> > This is an awkard way to print all odd numbers, have a look at this:
> >
> > #!/bin/bash
> >
> > LIMIT=10
> > a=1
> >
> > while test ${a} -lt ${LIMIT}
> > do
> > go=`expr ${a} % 2`
> > if test ${go} -ne 0
> > then
> > echo "Odd number ${a}"
> > fi
> >
> > a=`expr ${a} + 1`
> >
> > done
> >
> >
> > exit 0
> >
> >
> > In this example, if you change the LIMIT value to 100, the script will print
> > you all odds numbers, while in your example you have to put numbers by your
> > own. In perl it can result as:
> >
> > #!/usr/bin/perl
> >
> > $LIMIT=10;
> > $a=1;
> >
> > while( $a < $LIMIT ){
> > if( ($a % 2) !=0 ){
> > print "Odd number $a\n";
> > }
> > $a++;
> > }
> >
> > exit;
> >
> >
> > Luca
> >
> >
>
>

--
Charles D. (Chuck) Harding Voice: 925-423-8879
Senior Computer Associate ICCD/SDD/ICRMG Fax: 925-423-8719
Lawrence Livermore National Laboratory Computation Directorate
Livermore, CA USA http://www.llnl.gov GPG Public Key ID: B9EB6601
-- Three out of five people aren't the other two. --
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: bash question

am 02.04.2004 00:53:48 von Glynn Clements

Chuck Harding wrote:

> How about
>
> #!/bin/bash
> for ((i=1;i<11;i+=2))

I haven't heard of this before. It must have been introduced fairly
recently; it isn't supported by bash 2.03 or any 1.x version.

--
Glynn Clements
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: bash question

am 02.04.2004 11:32:08 von urgrue

> I haven't heard of this before. It must have been introduced fairly
> recently; it isn't supported by bash 2.03 or any 1.x version.

it works for me with bash 2.05b
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: bash question

am 03.04.2004 20:19:17 von Nico Schottelius

--ZARJHfwaSJQLOEUz
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Glynn Clements [Thu, Apr 01, 2004 at 11:53:48PM +0100]:
> Chuck Harding wrote:
>=20
> > How about
> >=20
> > #!/bin/bash
> > for ((i=3D1;i<11;i+=3D2))
>=20
> I haven't heard of this before. It must have been introduced fairly
> recently; it isn't supported by bash 2.03 or any 1.x version.

Just to point to another nice shell: zsh4.1.1 does support this
and much more.

I really don't understand why you all bind yourself to bash for scripting!

Bash is slow, big and comfortable. Scripts don't need comfort.

I always use 'ash' (much smaller/faster) for scripts and 'zsh' (much more
nice extensions and still at least as fast as bash) for normal use.

Nico

--=20
Keep it simple & stupid, use what's available.
pgp: 8D0E E27A | Nico Schottelius
http://nerd-hosting.net | http://linux.schottelius.org

--ZARJHfwaSJQLOEUz
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAbwAlzGnTqo0OJ6QRAgY9AKDQKnIzkFrDmRRbDn4aRI4VcGXlsACg u44X
e+9DieodwEbC57yM+t/YW5Y=
=Ybg6
-----END PGP SIGNATURE-----

--ZARJHfwaSJQLOEUz--
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: bash question

am 19.04.2004 03:07:02 von jvictor_rj

/usr/bin/seq 1 2 10
works fine

Scott@Charter wrote:

>I can't get this bash script to work. It's suppose to print all odd numbers
>from 1 to 10.
>
>#!/usr/bin/bash
>
>LIMIT=10
>a=1
>
>while [ "$a" -le $LIMIT ]
>do
> if [ "$a" -eq $(1 3 5 7 9) ] <---------Something not right here.
> then
> echo -n "$a "
> fi
>
>echo
>
>let "a+=1"
>done
>echo; echo
>exit 0
>
>
>Can anyone figure out what I am doing wrong? Can I also see the same thing
>written in Perl?
>
>Thanks for your help.
>
>
>
>Scott D. Smallsreed
>3030 Chipmunk Dr.
>Washoe Valley, NV 89704
>775.849.8411 Hm
>775.849.8412 Fax
>775.722.7773 Cell
>
>

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: bash question

am 19.04.2004 03:14:19 von jvictor_rj

Ooops!
must be a bash script...
sorry

Joao Victor A. Di Stasi wrote:

> /usr/bin/seq 1 2 10
> works fine
>
> Scott@Charter wrote:
>
>> I can't get this bash script to work. It's suppose to print all odd
>> numbers
>> from 1 to 10.
>>
>> #!/usr/bin/bash
>>
>> LIMIT=10
>> a=1
>>
>> while [ "$a" -le $LIMIT ]
>> do
>> if [ "$a" -eq $(1 3 5 7 9) ] <---------Something not right here.
>> then
>> echo -n "$a "
>> fi
>>
>> echo
>>
>> let "a+=1"
>> done
>> echo; echo
>> exit 0
>>
>>
>> Can anyone figure out what I am doing wrong? Can I also see the same
>> thing
>> written in Perl?
>>
>> Thanks for your help.
>>
>>
>>
>> Scott D. Smallsreed
>> 3030 Chipmunk Dr.
>> Washoe Valley, NV 89704
>> 775.849.8411 Hm
>> 775.849.8412 Fax
>> 775.722.7773 Cell
>>
>>
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-admin" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: bash question

am 19.04.2004 03:42:05 von Stephen Samuel

I'm going to presume that you want to search for a
specific set of numbers, and that the fact that the
fact that they're odd doesn't matter....

In that case, you'd have to use a for loop to seach
thru the list of 'important' values:

for $tval in 1 3 5 7 9; do
if [ $a -eq $tval ]
then
echo -n "$a "
fi
done

As far as I know, there's no fast way to search for
one value in a list of candidates.

Scott@Charter wrote:
> I can't get this bash script to work. It's suppose to print all odd numbers
> from 1 to 10.
>
> #!/usr/bin/bash
>
> LIMIT=10
> a=1
>
> while [ "$a" -le $LIMIT ]
> do
> if [ "$a" -eq $(1 3 5 7 9) ] <---------Something not right here.
> then
> echo -n "$a "
> fi
>
> echo
>
> let "a+=1"
> done
> echo; echo
> exit 0
>
>
> Can anyone figure out what I am doing wrong? Can I also see the same thing
> written in Perl?


--
Stephen Samuel +1(604)876-0426 samuel@bcgreen.com
http://www.bcgreen.com/~samuel/
Powerful committed communication. Transformation touching
the jewel within each person and bringing it to light.

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html