bash question
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 "
fiecho
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
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:[email protected]
REV:20040401T004530Z
END:VCARD
Quoted 11 lines · On Thursday 01 April 2004 02:45 Scott@Charter's cat walking on the keyboard…
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=1while [ "$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
Signature · 2 lines
Luca Ferrari, [email protected]
And just to show another way:
#!/bin/bash
for i in seq 1 10; do
if [ $((${i} % 2)) != 0 ]; then
echo ${i} fi
done
Quoted 59 lines · Luca Ferrari
Quoted 11 lines · On Thursday 01 April 2004 02:45 Scott@Charter's cat walking on the keyboard…
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=1while [ "$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=1while test ${a} -lt ${LIMIT}
do
go=expr ${a} % 2
if test ${go} -ne 0then 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
Signature · 3 lines
Jeff Largent ImageLinks, Inc. Sr System Admin Melbourne, Fl 32935 (321) 253-0011 fax: (321) 253-5559
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
Quoted 77 lines · Jeff Largent
And just to show another way:
#!/bin/bash
for i in
seq 1 10; doif [ $((${i} % 2)) != 0 ]; then echo ${i} fidone
Quoted 62 lines · Luca Ferrari
Quoted 11 lines · On Thursday 01 April 2004 02:45 Scott@Charter's cat walking on the keyboard…
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=1while [ "$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=1while test ${a} -lt ${LIMIT}
do
go=expr ${a} % 2
if test ${go} -ne 0then 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
Signature · 5 lines
Charles D. (Chuck) Harding <[email protected]> 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. --
Quoted 4 lines · Chuck Harding
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.
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 2 lines
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
Glynn Clements [Thu, Apr 01, 2004 at 11:53:48PM +0100]:
Quoted 9 lines
Quoted 4 lines · Chuck Harding
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
Signature · 3 lines
Keep it simple & stupid, use what's available. pgp: 8D0E E27A | Nico Schottelius http://nerd-hosting.net | http://linux.schottelius.org
/usr/bin/seq 1 2 10
works fine
Quoted 38 lines · 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=1while [ "$a" -le $LIMIT ]
do
if [ "$a" -eq $(1 3 5 7 9) ] <---------Something not right here.
thenecho -n "$a "fi
echo
let "a+=1"
done
echo; echo
exit 0Can 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
Ooops!
must be a bash script...
sorry
Quoted 49 lines · Joao Victor A. Di Stasi
/usr/bin/seq1 2 10
works fineQuoted 30 lines · 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=1while [ "$a" -le $LIMIT ]
do
if [ "$a" -eq $(1 3 5 7 9) ] <---------Something not right here.
thenecho -n "$a "fi
echo
let "a+=1"
done
echo; echo
exit 0Can anyone figure out what I am doing wrong? Can I also see the same thing written in Perl?
Thanks for your help.
Quoted 8 lines
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
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.
Quoted 25 lines · 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=1while [ "$a" -le $LIMIT ]
do
if [ "$a" -eq $(1 3 5 7 9) ] <---------Something not right here.then echo -n "$a " fiecho
let "a+=1"
done
echo; echo
exit 0Can anyone figure out what I am doing wrong? Can I also see the same thing written in Perl?
Signature · 4 lines
Stephen Samuel +1(604)876-0426 [email protected] http://www.bcgreen.com/~samuel/ Powerful committed communication. Transformation touching the jewel within each person and bringing it to light.