* bash question
@ 2004-04-01 0:45 Scott@Charter
2004-04-01 7:03 ` Luca Ferrari
` (2 more replies)
0 siblings, 3 replies; 10+ messages in thread
From: Scott@Charter @ 2004-04-01 0:45 UTC (permalink / raw)
To: Linux-Admin-Group
[-- Attachment #1: Type: text/plain, Size: 535 bytes --]
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
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Scott Smallsreed.vcf --]
[-- Type: text/x-vcard; name="Scott Smallsreed.vcf", Size: 366 bytes --]
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=QUOTED-PRINTABLE:3030 Chipmunk Dr.=0D=0AWashoe Valley, Nevada 89704=0D=0AUS
EMAIL;PREF;INTERNET:scott.smallsreed@mindspring.com
REV:20040401T004530Z
END:VCARD
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: bash question
2004-04-01 0:45 bash question Scott@Charter
@ 2004-04-01 7:03 ` Luca Ferrari
2004-04-01 14:58 ` Jeff Largent
2004-04-19 1:07 ` Joao Victor A. Di Stasi
2004-04-19 1:42 ` Stephen Samuel
2 siblings, 1 reply; 10+ messages in thread
From: Luca Ferrari @ 2004-04-01 7:03 UTC (permalink / raw)
To: linux-admin
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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: bash question
2004-04-01 7:03 ` Luca Ferrari
@ 2004-04-01 14:58 ` Jeff Largent
2004-04-01 19:46 ` Chuck Harding
0 siblings, 1 reply; 10+ messages in thread
From: Jeff Largent @ 2004-04-01 14:58 UTC (permalink / raw)
To: linux-admin
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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: bash question
2004-04-01 14:58 ` Jeff Largent
@ 2004-04-01 19:46 ` Chuck Harding
2004-04-01 22:53 ` Glynn Clements
0 siblings, 1 reply; 10+ messages in thread
From: Chuck Harding @ 2004-04-01 19:46 UTC (permalink / raw)
To: linux-admin
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 <charding@llnl.gov> 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. --
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: bash question
2004-04-01 19:46 ` Chuck Harding
@ 2004-04-01 22:53 ` Glynn Clements
2004-04-02 9:32 ` urgrue
2004-04-03 18:19 ` Nico Schottelius
0 siblings, 2 replies; 10+ messages in thread
From: Glynn Clements @ 2004-04-01 22:53 UTC (permalink / raw)
To: Chuck Harding; +Cc: linux-admin
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 <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: bash question
2004-04-01 22:53 ` Glynn Clements
@ 2004-04-02 9:32 ` urgrue
2004-04-03 18:19 ` Nico Schottelius
1 sibling, 0 replies; 10+ messages in thread
From: urgrue @ 2004-04-02 9:32 UTC (permalink / raw)
To: admin
> 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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: bash question
2004-04-01 22:53 ` Glynn Clements
2004-04-02 9:32 ` urgrue
@ 2004-04-03 18:19 ` Nico Schottelius
1 sibling, 0 replies; 10+ messages in thread
From: Nico Schottelius @ 2004-04-03 18:19 UTC (permalink / raw)
To: Glynn Clements; +Cc: Chuck Harding, linux-admin
[-- Attachment #1: Type: text/plain, Size: 826 bytes --]
Glynn Clements [Thu, Apr 01, 2004 at 11:53:48PM +0100]:
> 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.
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
--
Keep it simple & stupid, use what's available.
pgp: 8D0E E27A | Nico Schottelius
http://nerd-hosting.net | http://linux.schottelius.org
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: bash question
2004-04-01 0:45 bash question Scott@Charter
2004-04-01 7:03 ` Luca Ferrari
@ 2004-04-19 1:07 ` Joao Victor A. Di Stasi
2004-04-19 1:14 ` Joao Victor A. Di Stasi
2004-04-19 1:42 ` Stephen Samuel
2 siblings, 1 reply; 10+ messages in thread
From: Joao Victor A. Di Stasi @ 2004-04-19 1:07 UTC (permalink / raw)
To: linux-admin
/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
>
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: bash question
2004-04-19 1:07 ` Joao Victor A. Di Stasi
@ 2004-04-19 1:14 ` Joao Victor A. Di Stasi
0 siblings, 0 replies; 10+ messages in thread
From: Joao Victor A. Di Stasi @ 2004-04-19 1:14 UTC (permalink / raw)
Cc: linux-admin
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
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: bash question
2004-04-01 0:45 bash question Scott@Charter
2004-04-01 7:03 ` Luca Ferrari
2004-04-19 1:07 ` Joao Victor A. Di Stasi
@ 2004-04-19 1:42 ` Stephen Samuel
2 siblings, 0 replies; 10+ messages in thread
From: Stephen Samuel @ 2004-04-19 1:42 UTC (permalink / raw)
To: Scott@Charter; +Cc: Linux-Admin-Group
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.
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2004-04-19 1:42 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-04-01 0:45 bash question Scott@Charter
2004-04-01 7:03 ` Luca Ferrari
2004-04-01 14:58 ` Jeff Largent
2004-04-01 19:46 ` Chuck Harding
2004-04-01 22:53 ` Glynn Clements
2004-04-02 9:32 ` urgrue
2004-04-03 18:19 ` Nico Schottelius
2004-04-19 1:07 ` Joao Victor A. Di Stasi
2004-04-19 1:14 ` Joao Victor A. Di Stasi
2004-04-19 1:42 ` Stephen Samuel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).