Re: bash question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxx>  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. --
-
: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Newbie]     [Audio]     [Hams]     [Kernel Newbies]     [Util Linux NG]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Device Drivers]     [Samba]     [Video 4 Linux]     [Git]     [Fedora Users]

  Powered by Linux