help with iteration

help with iteration

Post by Adam Bozanic » Wed, 31 Dec 2003 10:37:00



Hi all.  I am trying to loop over each line of output from a command like
this:

#!/bin/sh
fstypes='ext2fs msdos ntfs'
for fs in $fstypes
do
    lines=$( mount | grep $fs )
    for line in $dirs
    do
        echo $line
    done
done

I can't seem to get the behavior that I want.  The inner loop is either
delimeting on spaces, or if I use quotes, it gets the whole thing.

In perl, this does what I want:

#!/usr/bin/perl -w

open( FH , "mount | grep ext2fs |" ) || die("can't open");

while( <FH> ){
    print;

Quote:}

Is there any way to loop over each line of output like that in sh?

Thanks in advance

-Adam

 
 
 

help with iteration

Post by diva_pr at yahoo dot co » Wed, 31 Dec 2003 15:07:29



> Hi all.  I am trying to loop over each line of output from a command like
> this:

> #!/bin/sh
> fstypes='ext2fs msdos ntfs'
> for fs in $fstypes
> do
>     lines=$( mount | grep $fs )
>     for line in $dirs
>     do
>         echo $line
>     done
> done

#!/bin/sh
fstypes='ext2fs|msdos|ntfs'
mount -v| egrep "$fstypes" | while read line
do
     echo "$line"
done

Tested on Fedora Core/Solaris(2.8) with bash(Ver>=2.05b.0(1))

hope this helps
Divakar

 
 
 

help with iteration

Post by William Par » Wed, 31 Dec 2003 16:20:21



> Hi all.  I am trying to loop over each line of output from a command like
> this:

> #!/bin/sh
> fstypes='ext2fs msdos ntfs'
> for fs in $fstypes
> do
>     lines=$( mount | grep $fs )
>     for line in $dirs
>     do
>         echo $line
>     done
> done

1.  for fs in ext2fs msdos ntfs; do
        mount | grep $fs | while read line; do
            echo $line
        done
    done

2.  mount | egrep 'ext2fs|msdos|ntfs' | while read line; do
        echo $line
    done

3.  mount | while read line; do
        case "$line" in
            *ext2fs*) echo $line ;;
            *msdos*) echo $line ;;
            *ntfs*) echo $line ;;
        esac
    done

--

Linux solution for data management and processing.

 
 
 

help with iteration

Post by Adam Bozanic » Thu, 01 Jan 2004 18:27:53


On 30 Dec 2003 07:20:21 GMT



> > Hi all.  I am trying to loop over each line of output from a command like
> > this:

> > #!/bin/sh
> > fstypes='ext2fs msdos ntfs'
> > for fs in $fstypes
> > do
> >     lines=$( mount | grep $fs )
> >     for line in $dirs
> >     do
> >         echo $line
> >     done
> > done

> 1.  for fs in ext2fs msdos ntfs; do
>    mount | grep $fs | while read line; do
>        echo $line
>    done
>     done

> 2.  mount | egrep 'ext2fs|msdos|ntfs' | while read line; do
>    echo $line
>     done

> 3.  mount | while read line; do
>    case "$line" in
>        *ext2fs*) echo $line ;;
>        *msdos*) echo $line ;;
>        *ntfs*) echo $line ;;
>    esac
>     done

Thanks diva_pr and William.  Just what I needed.

-Adam

 
 
 

1. 2-array iteration on sh??

I have been learning the Bourne shell lately, writing simple
scripts, but there is a feature that just doesn't seem to be
there: the ability to iterate on -or traverse- not one, but
two arrays.

A couple of examples will illustrate:

--------------------------------------------------------------
in C:

        for (i = 1; i <= last; i++) {
                this[i] = that[i] + 1;
                }

--------------------------------------------------------------
in C shell:

set city = `awk '{print $1}' table`
set distance = `awk '{print $2}' table`

set i = 1
set last = ${#city}

while ($i <= $last)
        set destination = $city[$i]
        set miles = $distance[$i]
        if ($miles < 100) echo $destination is only $miles from here

end
--------------------------------------------------------------

Would sone of the 'sh' gurus out there please illustrate me
and show me how to write the above program using the Bourne shell?

Regards,

-Ramon

2. Linux META-FAQ

3. Modernize sched.h iteration macros

4. XDM: Cannot login after installing XFree 3.1.2G

5. [3/4] eliminate proc_fill_super() tasklist iteration

6. Printing Serially to Dot Matrix

7. Question regarding IFS and set -- : Why does it take two iterations?

8. Fortran-callable date function

9. AWK Iteration

10. Shell Iteration

11. break one loop and skip one iteration of outer loop

12. Iteration with "for" command?

13. sc621 problem (still changing after 9 iterations)